使用C#中的嵌套数组反序列化JSON [英] Deserializing JSON with nested arrays in C#
本文介绍了使用C#中的嵌套数组反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里尝试反序列化此JSON时遇到了麻烦:
I'm having trouble trying to deserialize this JSON here:
{
"response": {
"numfound": 1,
"start": 0,
"docs": [
{
"enID": "9999",
"startDate": "2013-09-25",
"bName": "XXX",
"pName": "YYY",
"UName": [
"ZZZ"
],
"agent": [
"BobVilla"
]
}
]
}
}
为此创建的类是:
public class ResponseRoot {
public Response response;
}
public class Response {
public int numfound { get; set; }
public int start { get; set; }
public Docs[] docs;
}
public class Docs {
public string enID { get; set; }
public string startDate { get; set; }
public string bName { get; set; }
public string pName { get; set; }
public UName[] UName;
public Agent[] agent;
}
public class UName {
public string uText { get; set; }
}
public class Agent {
public string aText { get; set; }
}
但是,每当我打电话给我
But, whenever I call:
ResponseRoot jsonResponse = sr.Deserialize<ResponseRoot>(jsonString);
jsonResponse最终为空,并且不反序列化JSON.我似乎无法说出为什么我的类可能与此JSON错误.
jsonResponse ends up being null and the JSON isn't deserialized. I can't seem to tell why my classes may be wrong for this JSON.
推荐答案
您的代码建议Docs
的UName
属性是对象数组,但它是json中的字符串数组,对于
your code suggest that the UName
Property of Docs
is an array of objects, but it's an array of strings in json, same goes for agent
尝试一下:
public class Docs
{
public string enID { get; set; }
public string startDate { get; set; }
public string bName { get; set; }
public string pName { get; set; }
public string[] UName;
public string[] agent;
}
并删除UName
和Agent
类
这篇关于使用C#中的嵌套数组反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文