JsonConvert.Deserialize not recognizing array

左心房为你撑大大i 提交于 2021-01-28 23:00:06

问题


I'm a little stumped on this one. Everything I do to check this out says it is a valid Json array, but JsonConvert.Deserialize says it is an object. Can someone point out what I'm doing wrong?

Code to replicate:

var data = "[{\"User\": {\"Identifier\": \"24233\",\"DisplayName\": \"Commerce Test Student\",\"EmailAddress\": \"email@email.ca\",\"OrgDefinedId\": \"UniqueId1\",\"ProfileBadgeUrl\": null,\"ProfileIdentifier\": \"zzz123\"},\"Role\": {\"Id\": 153,\"Code\": null,\"Name\": \"Commerce Student\"}}]";

var items = JsonConvert.DeserializeObject<List<T>>(data);

Where T is an object that matches the format below:

public class OrgUnitUser
{
    public User User { get; set; }
    public RoleInfo Role { get; set; }
}

public class User
{
    public string Identifier { get; set; }
    public string DisplayName { get; set; }
    public string EmailAddress { get; set; }
    public string OrgDefinedId { get; set; }
    public string ProfileBadgeUrl { get; set; }
    public string ProfileIdentifier { get; set; }
}
public class RoleInfo
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

It results in an error

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CoverPages.Models.D2L.OrgUnitUser]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Any/all help is appreciated!


回答1:


var data = "[{\"User\": {\"Identifier\": \"24233\",\"DisplayName\": \"Commerce Test Student\",\"EmailAddress\": \"email@email.ca\",\"OrgDefinedId\": \"UniqueId1\",\"ProfileBadgeUrl\": null,\"ProfileIdentifier\": \"zzz123\"},\"Role\": {\"Id\": 153,\"Code\": null,\"Name\": \"Commerce Student\"}}]";

public class User
{
    public string Identifier { get; set; }
    public string DisplayName { get; set; }
    public string EmailAddress { get; set; }
    public string OrgDefinedId { get; set; }
    public object ProfileBadgeUrl { get; set; }
    public string ProfileIdentifier { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public object Code { get; set; }
    public string Name { get; set; }
}

public class RootObject
{
    public User User { get; set; }
    public Role Role { get; set; }
}

var items = JsonConvert.DeserializeObject<List<RootObject>>(data);

or

var items = JsonConvert.DeserializeObject<List<RootObject>>(data)[0];

Try this code I thinks he working good

result:




回答2:


Thanks to Taras for the confirmation, but there is nothing wrong with the code itself.

When using a generic in JsonConver.Deserialize, it gives the error I listed above, however putting in the actual type of OrgUnitUser into the list, rather than T results in the convert succeeding.

Changing the code from

var items = JsonConvert.DeserializeObject<List<T>>(data);

to

var items = JsonConvert.DeserializeObject<List<OrgUnitUser>>(data);

Fixed the issue



来源:https://stackoverflow.com/questions/34183102/jsonconvert-deserialize-not-recognizing-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!