2017-06-22 69 views
0

我试图解析的多级JSON数组,但我得到以下异常:如何解析的多级JSON阵列

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Tractor.Models.UserDetails' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. 

这里是JSON响应:

{ 
    "status": 1, 
    "message": null, 
    "userdetails": [ 
    { 
     "ID": 9, 
     "Name": "aleem", 
     "Company_Name": null, 
     "Email": null, 
     "Password": null, 
     "Created_Date": null, 
     "Email_Confirm": null, 
     "Token": null, 
     "Phone": null, 
     "Website": null, 
     "allcompanies": [ 
     { 
      "Name": "self", 
      "allprojects": [ 
      { 
       "ID": 1, 
       "Name": "test" 
      } 
      ] 
     } 
     ], 
     "screens": 3 
    } 
    ] 
} 

下面是我已经添加到获得JSON响应的类:

class LoginApiResponse 
{ 
     [JsonProperty("status")] 
     public int Status { get; set; } 

     [JsonProperty("message")] 
     public string Message { get; set; } 

     [JsonProperty("userdetails")] 
     public UserDetails UserDetails { get; set; } 

    } 

class UserDetails 
{ 
    [JsonProperty("ID")] 
    public int ID { get; set; } 

    [JsonProperty("Name")] 
    public string Name { get; set; } 

    [JsonProperty("Company_Name")] 
    public string Company_Name { get; set; } 

    [JsonProperty("Email")] 
    public string Email { get; set; } 

    [JsonProperty("Password")] 
    public string Password { get; set; } 

    [JsonProperty("Created_Date")] 
    public string Created_Date { get; set; } 

    [JsonProperty("Email_Confirm")] 
    public string Email_Confirm { get; set; } 

    [JsonProperty("Token")] 
    public string Token { get; set; } 

    [JsonProperty("Phone")] 
    public string Phone { get; set; } 

    [JsonProperty("Website")] 
    public string Website { get; set; } 

    [JsonProperty("allcompanies")] 
    public List<Company> Companies { get; set; } 

    [JsonProperty("screens")] 
    public int Screens { get; set; } 
} 

class Company 
    { 
     [JsonProperty("Name")] 
     public string Name { get; set; } 

     [JsonProperty("allprojects")] 
     public List<Project> Projects {get;set;} 
    } 

class Project 
    { 
     [JsonProperty("ID")] 
     public int ID { get; set; } 

     [JsonProperty("Name")] 
     public string Name { get; set; } 
    } 

我正在使用下面的代码来序列化此JSON:

var response = client.Execute(request); 
var final = JsonConvert.DeserializeObject<LoginApiResponse>(response.Content); 
+0

'UserDetails'是Json中的数组 – PinBack

回答

1

的JSON要求UserDetails字段为UserDetails对象数组(或列表):

class LoginApiResponse 
{ 
    //......... 
    [JsonProperty("userdetails")] 
    public UserDetails[] UserDetails { get; set; } 
    //    ^^ 
    // Alternatively, use List<UserDetails> instead of UserDetails[] 
} 
+0

感谢它为我工作。 – Aleem

-1

我做的方式,它是反序列化JSON对象为动态变量,然后访问属性您需要使用该动态变量,如果需要,可以使用这些来初始化模型。

0

你的问题是在LoginApiResponse类。

该属性UserDetails应该是List<UserDetails>而不是UserDetails。所以,你得到:

class LoginApiResponse 
{ 
    [JsonProperty("status")] 
    public int Status { get; set; } 

    [JsonProperty("message")] 
    public string Message { get; set; } 

    [JsonProperty("userdetails")] 
    public List<UserDetails> UserDetails { get; set; } 

} 

这是因为你期望在JSON用于userdetails数组/列表:

{ 
    "status": 1, 
    "message": null, 
    "userdetails": [ 
     { /* ... */ }, 
     { /* ... */ } 
    ] 
} 

或者(如果你不希望的UserDetails的列表),你可以更改json(从列表到对象):

{ 
    "status": 1, 
    "message": null, 
    "userdetails": 
     { /* ... */ } 
} 
+0

谢谢,它为我工作。 – Aleem