[英]Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List
[英]JSON .NET - annot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1
我阅读了很多有关此问题的主题,但找不到解决方案。 我有AP返回的JSON
{
"Version": "XXX",
"StatusCode": 400,
"RequestId": "XXX",
"Success": false,
"ValidationMessages": {
"SomeTable[0].PropName": [
"This field is invalid"
],
"SomeTable[1].PropName": [
"This field is invalid"
],
"SomeTable[0].OtherProp": [
"This field is invalid"
]
},
"Result": {}
}
我创建了课程:
public class MyResponse
{
public string Version { get; set; }
public string StatusCode { get; set; }
public string RequestId { get; set; }
public bool Success { get; set; }
public SomeResult Result { get; set; }
public List<KeyValuePair<string, List<string>>> ValidationMessages { get; set; }
}
尝试反序列化JSON时,标题出现错误。 如何实现动态ValidationMessages的序列化?
对于您的特定情况,您可以使用Dictionary而不是List,因为Dictionary实现了一个对象并接收一个键值对,您可以拥有一个动态属性名称以及所需的任何值(字符串列表)
public class MyResponse
{
public string Version { get; set; }
public string StatusCode { get; set; }
public string RequestId { get; set; }
public bool Success { get; set; }
public SomeResult Result { get; set; }
public Dictionary<string, List<string>> ValidationMessages {get;set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.