JsonConvert.Deserialize无法识别数组

时间:2015-12-09 15:49:27

标签: c# json json.net json-deserialization

我对这个有点难过。我所做的一切都是为了检查这个,它说它是一个有效的Json数组,但是JsonConvert.Deserialize说它是一个对象。有人可以指出我做错了吗?

要复制的代码:

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);

其中T是符合以下格式的对象:

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; }
}

导致错误

Newtonsoft.Json.JsonSerializationException:无法将当前JSON对象(例如{&#34; name&#34;:&#34; value&#34;})反序列化为类型&System; System.Collections.Generic.List `1 [CoverPages.Models.D2L.OrgUnitUser]&#39;因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。

感谢任何/所有帮助!

2 个答案:

答案 0 :(得分: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);

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

试试这段代码我觉得他工作得很好

结果: enter image description here

答案 1 :(得分:0)

感谢Taras的确认,但代码本身没有任何问题。

在JsonConver.Deserialize中使用泛型时,它会给出我上面列出的错误,但是将实际类型的OrgUnitUser放入列表中,而不是T会导致转换成功。

更改代码
//in MyUIManager:
        @Override
        public void showStartSignIn(GitkitUser.UserProfile userProfile) {
            //Show start login fragment
        }
//some where in the login fragment:
        @Override
        public void onClick(View view) {
            StartSignInRequest request = new StartSignInRequest();
            request.setEmail(email);
            request.setIdProvider(idProvider);
            mRequestHandler.handle(request);
        }

WITH       Total AS (   SELECT CrewDesc, COUNT(*) AS [Count]
                FROM database.emp 
                INNER JOIN database.crew on sim1 = sim2
                GROUP BY CrewDesc 
               ), 
       Attd AS (    SELECT DeptDescription, COUNT(*) AS [Count]
                FROM database.Attendee
                GROUP BY DeptDescription  
             )

SELECT  COALESCE(CrewDesc,DeptDescription) AS [Dept],
       Total.[Count] AS [Total#],Attd.[Count] AS [Attd],
       Total.[Count] * Datepart(ww, GetDate()) AS [(Total*Week)], 
       CAST(Attd.[Count] AS VARCHAR(10))+'/'+ CAST((Total.[Count] * Datepart(ww, GetDate()))AS VARCHAR(10))  AS [Attd/(Total*week)%]
 FROM Total INNER JOIN Attd ON Total.CrewDesc = Attd.DeptDescription 

修正了问题