反序列化对象 returns null 尝试映射时嵌套 JSON
Deserialize Object returns null with nested JSON when trying to map it
我知道这个问题看起来很熟悉,但就我而言,有点不同。
- 我已经使用 API returns 格式的 XML 数据。
- 获取 API 响应并将其转换为 JSON 格式,以便我能够进行映射。
我的问题,
当我 DeserializeObject 它总是 returns null 并且我不太确定这是否是由映射引起的。
问题:如何反序列化嵌套的 JSON 对象以映射字段?如果我必须在将 XML 转换为 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 对象
{
"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#生成class基于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; }
}
简单地反序列化到根 class Welcome
而不是 AccountLite
.
//var des = (AccountLite)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(AccountLite));
Welcome welcome = JsonConvert.DeserializeObject<Welcome>(json);
我知道这个问题看起来很熟悉,但就我而言,有点不同。
- 我已经使用 API returns 格式的 XML 数据。
- 获取 API 响应并将其转换为 JSON 格式,以便我能够进行映射。
我的问题, 当我 DeserializeObject 它总是 returns null 并且我不太确定这是否是由映射引起的。
问题:如何反序列化嵌套的 JSON 对象以映射字段?如果我必须在将 XML 转换为 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 对象
{
"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#生成class基于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; }
}
简单地反序列化到根 class Welcome
而不是 AccountLite
.
//var des = (AccountLite)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(AccountLite));
Welcome welcome = JsonConvert.DeserializeObject<Welcome>(json);