[英]Deserialize Object returns null with nested JSON when trying to map it
I know this question looks familiar but in my case, it is a bit different.我知道这个问题看起来很熟悉,但就我而言,它有点不同。
My problem, When I DeserializeObject it always returns null and I am not too sure if this is been caused by the mapping.我的问题,当我 DeserializeObject 它总是返回 null 并且我不太确定这是否是由映射引起的。
Question : How can I deserialize nested JSON objects for mapping the fields?问题:如何反序列化嵌套的 JSON 对象以映射字段? Also please guide me if I have to change something in terms of converting XML to JSON
如果我必须在将 XML 转换为 JSON 方面进行更改,也请指导我
Thanks!谢谢!
Converting XML response to JSON将 XML 响应转换为 JSON
var result = await client.GetAsync($"url");
var xml = result.Content.ReadAsStringAsync().Result;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
//the results hear is null for all the properties
var des = (AccountLite)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(AccountLite));
Console.WriteLine(des);
Console.ReadLine();
JSON Object JSON 对象
{
"MessageEnvelope": {
"ListOfAccountLite": {
"AccountLite": [
{
"Id": "",
"AccountStatus": "",
"AccountTypeCode": "",
"CSN": "",
"Location": "",
"MasterAccountId": "",
"Name": ""
},
{
"Id": "",
"AccountStatus": "",
"AccountTypeCode": "",
"CSN": "",
"Location": "",
"MasterAccountId": "",
"Name": ""
},
{
"Id": "",
"AccountStatus": "",
"AccountTypeCode": "",
"CSN": "",
"Location": "",
"MasterAccountId": "",
"Name": ""
}
]
}
}
}
C# Generated class based on the JSON object C#基于JSON对象生成的类
public partial class Welcome
{
[JsonProperty("MessageEnvelope")]
public MessageEnvelope MessageEnvelope { get; set; }
}
public partial class MessageEnvelope
{
[JsonProperty("ListOfAccountLite")]
public ListOfAccountLite ListOfAccountLite { get; set; }
}
public partial class ListOfAccountLite
{
[JsonProperty("AccountLite")]
public AccountLite[] AccountLite { get; set; }
}
public partial class AccountLite
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("AccountStatus")]
public string AccountStatus { get; set; }
[JsonProperty("AccountTypeCode")]
public string AccountTypeCode { get; set; }
[JsonProperty("CSN")]
public string Csn { get; set; }
[JsonProperty("Location")]
public string Location { get; set; }
[JsonProperty("MasterAccountId")]
public string MasterAccountId { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
}
Simply deserialize to the root class Welcome
instead of AccountLite
.只需反序列化为根类
Welcome
而不是AccountLite
。
//var des = (AccountLite)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(AccountLite));
Welcome welcome = JsonConvert.DeserializeObject<Welcome>(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.