【发布时间】:2020-07-17 02:23:54
【问题描述】:
我有如下所示的 json(odata 响应),我正在尝试在 C# 中对其进行反序列化。响应如下所示:
{
"d": {
"results": [
{
"__metadata": {
"id": "http://localhost:7305",
"uri": "http://localhost:7305",
"type": "Subject.Classifications"
},
"dateTime": "2020-06-25T11:31:51",
"source": "JHGTY",
"body": "The offering has now increased ",
"subject": "Offering- updated",
"fees": {
"results": [
{
"week": "1",
"amount":"90.00"
},
{
"week":"2",
"amount":"90.00"
}
]
}
"Error": {
"__deferred": {
"uri": "http://localhost:7305/"
}
}
},
{
"__metadata": {
"id": "http://localhost:7305",
"uri": "http://localhost:7305",
"type": "Subject.Classifications"
},
"dateTime": "2020-06-26T11:25:51",
"source": "XFGFT",
"body": "The offering has now degraded ",
"subject": "Offering- updated",
"Error": {
"__deferred": {
"uri": "http://localhost:7305/"
}
}
}
]
}
}
我正在使用以下代码使用 newtonsoft 对其进行反序列化:
dynamic notifications = JsonConvert.DeserializeObject<Root>(response.Content);
我已经创建了像下面这样的模型,并且能够对其进行反序列化并获得结果。
public class Root
{
[JsonProperty("d")]
public D D { get; set; }
}
public class D
{
[JsonProperty("results")]
public List<Student> Results { get; set; }
}
public class Student
{
public string body{ get; set; }
public string subject{ get; set; }
public List<Fee> fees { get; set; }
}
public class Fee
{
public string week{ get; set; }
public string amount{ get; set; }
}
当我尝试获取费用数组时出现以下错误,但如果我从学生班级中删除费用属性,它工作正常。
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type
'System.Collections.Generic.List`1[genie.Api.Controllers.ClassificationController+Classifications]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
【问题讨论】:
标签: c# json json.net deserialization