简体   繁体   English

反序列化包含对象列表的Json

[英]Deserialize a Json containing a list of object

I work on an UniversalApp that call a lot of webservices returning JSON datas. 我在UniversalApp上工作,该应用程序调用了许多返回JSON数据的Web服务。

All the webservices return me a first level of Json ( WsResponse.cs ), that contained this structure: 所有的Web服务都向我返回了Json的第一级( WsResponse.cs ),其中包含以下结构:

public string status { get; set; }
public object data { get; set; }
public List<object> errors { get; set; }

If the status is " success ", I can deserialize the simple data object by: 如果状态为“ 成功 ”,则可以通过以下方式反序列化简单数据对象:

WsResponse wsResponse = JsonConvert.DeserializeObject<WsResponse>(response);    
User user = JsonConvert.DeserializeObject<User>(wsResponse.data.ToString());

In other cases, it is necessary to deserialize first in a result object, before to deserialize the datas: 在其他情况下,必须先在结果对象中反序列化,然后再反序列化数据:

WsResponse wsResponse = JsonConvert.DeserializeObject<WsResponse>(response);
WsResponseResult wsResponseResult = JsonConvert.DeserializeObject<WsResponseResult>(
                                                            wsResponse.data.ToString());
List<Theme> themes = JsonConvert.DeserializeObject<List<Theme>>(
                                                    wsResponseResult.result.ToString());

But if the status is " failure ", I need to catch the errors list. 但是,如果状态为“ failure ”,则需要捕获错误列表。 But I can't deserialize this object list... Here is an example of JSON datas that I get from webservice when it contains errors: 但是我无法反序列化此对象列表...这是一个当Web服务包含错误时从Web服务获取的JSON数据的示例:

{
  "status": "failure",
  "data": {},
  "errors": [
    {
      "200": "The parameter 'email' is not present"
    },
    {
      "200": "The parameter 'password' is not present"
    }
  ]
}

I try to deserialize it by: 我尝试通过以下方式反序列化:

List<KeyValuePair<string, string>> kvpError = new List<KeyValuePair<string,string>>();
kvpError = JsonConvert.DeserializeObject<List<KeyValuePair<<string, string>>>(
                                                            wsResponse.errors.ToString());

Dictionary<string, string> dError = new Dictionary<string, string>();
dError = JsonConvert.DeserializeObject<Dictionary<string, string>>(
                                                            wsResponse.errors.ToString());

=> but it doesn't work =>但它不起作用

I also try to through the error datas before deserializing them: 我还尝试对错误数据进行反序列化之前将它们反序列化:

foreach(var error in wsResponse.errors)
{                        
    KeyValuePair<string, string> kvpError = new KeyValuePair<string,string>();
    kvpError = JsonConvert.DeserializeObject<KeyValuePair<string, string>>(
                                                                        error.ToString());
}

=> but it doesn't work better... =>但效果不佳...

Would you have an idea on how to solve my problem? 您对如何解决我的问题有想法吗?

The way your JSON is structured, errors is actually a List<Dictionary<string, string>> . JSON的结构errors实际上是一个List<Dictionary<string, string>> If you use that instead of List<object> , it will deserialize the errors properly: 如果使用它代替List<object> ,它将正确反序列化错误:

public class Response
{
    public string status { get; set; }
    public object data { get; set; }
    public List<Dictionary<string, string>> errors { get; set; }
}

Yields: 产量:

反序列化结果

Side note - If you have any control of the JSON structure, you're better off with a single List<KeyValuePair<string, string>> which you can deserialize. 旁注-如果您可以控制JSON结构,最好使用单个List<KeyValuePair<string, string>>进行反序列化。 Also, instead of using object data , actually pass the User properties inside the JSON so you can save yourself the double deserialization. 另外,实际上不传递object data ,而是在JSON内传递User属性,这样您就可以避免双重反序列化。

If you wanted to make it possible to deserialize this to a List<KeyValuePair<string, string>> , your JSON would need to look like this: 如果您希望将其反序列化为List<KeyValuePair<string, string>> ,则您的JSON需要如下所示:

{
  "status": "failure",
  "data": {},
  "errors": [
    {
      "Key": "200", "Value": "The parameter 'email' is not present"
    },
    {
      "Key": "200", "Value": "The parameter 'password' is not present"
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM