简体   繁体   中英

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

{
    "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. How can I achieve serialization of dynamic 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM