简体   繁体   English

JSON .NET-将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1

[英]JSON .NET - annot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1

I read a lot of topics about this problem but couldn't find a solution. 我阅读了很多有关此问题的主题,但找不到解决方案。 I have JSON returned by AP 我有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": {}
}

I created class: 我创建了课程:

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; }
}

When I am trying to deserialize JSON, I am getting an error from the title. 尝试反序列化JSON时,标题出现错误。 How can I achieve serialization of dynamic ValidationMessages? 如何实现动态ValidationMessages的序列化?

For your specific scenario you can use a Dictionary instead a List since dictionary implements an object and receives a key value pair, you can have a dynamic property name and whatever value you need (List of strings) 对于您的特定情况,您可以使用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.

相关问题 无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型&#39;System.Collections.Generic.List - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List 无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1&#39; - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1' JSON.Net - 无法将当前json对象(例如{“name”:“value”})反序列化为类型&#39;system.collections.generic.list`1 - JSON.Net - cannot deserialize the current json object (e.g. {“name”:“value”}) into type 'system.collections.generic.list`1 错误:无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型 'System.Collections.Generic.List`1 - Error: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 JsonSerializationException:无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections。 - JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.ICollection`1 [System.Double]” - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.ICollection`1[System.Double]' 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.IEnumerable`1 [WebApplication1.UserInfo]” - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.IEnumerable`1[WebApplication1.UserInfo]' 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 &#39;System.Collections.Generic.IList 问题 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList problem 无法将JSON对象反序列化为“System.Collections.Generic.List”类型 - Cannot deserialize JSON object into type 'System.Collections.Generic.List'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM