【发布时间】:2021-11-27 08:15:07
【问题描述】:
我之前从服务器获得了这个 JSON 字符串
"[
{
\"name":\"XYZ",
\"age\":\"75\",
\"height\":\"170.1\",
\"weight\":\"69.6\",
\"dob\":\"2000-10-07T07:23:26.876Z\"
},
{
\"name":\"ABC",
\"age\":\"15\",
\"height\":\"160.1\",
\"weight\":\"55.6\",
\"dob\":\"1990-10-07T07:23:26.876Z\"
},
]"
为此我使用了这样的类
public class Person
{
[JsonProperty("name")]
public string Name {get; set;}
[JsonProperty("age")]
public decimal Age {get; set;}
[JsonProperty("height")]
public decimal Height {get; set;}
[JsonProperty("weight")]
public decimal Weight {get; set;}
[JsonProperty("dob")]
public DateTime DOB {get; set;}
}
我已经反序列化了它使用
JsonConvert.DeserializeObject<Person[]>(jsonString)
但是现在服务器把JSON改成这样了
"{
\"XYZ":
{
\"age\":\"75\",
\"height\":\"170.1\",
\"weight\":\"69.6\",
\"DOB\":\"2000-10-07T07:23:26.876Z\"
},
\"ABC":
{
\"age\":\"15\",
\"height\":\"160.1\",
\"weight\":\"55.6\",
\"DOB\":\"1990-10-07T07:23:26.876Z\"
}
}"
name 属性被移除,取而代之的是根元素。我尝试将课程更改为这样,但它不起作用。如何反序列化它?
public class PersonResult
{
public Person [] Persons {get; set;}
}
【问题讨论】:
-
如果你有来自服务器的那个字符串,它看起来是双重编码的。如果您在从服务器获取文本后将该文本从调试器中拉出,请注意不要尝试删除实际上不存在的反斜杠,它们是由调试器显示工具提示插入的 VS
-
你说得对,我从调试器中复制了字符串。