简体   繁体   中英

Deserializing a JSON Array to a Dictionary<string, List<string>>

My ASP.NET program receive the Following JSON string:

{
    "key1" : "value1"
    "fileName" : [
        "filename1.docx",
        "filename2.xlsx"
    ]
}

I try to deserialize the JSON string with this line

var values = JsonConvert.DeserializeObject<Dictionary<String, List<String>>>(json)

But I get this error

Error Converting value "key1" to type 'System.Collections.Generic.List'

Any solution?

Create a class like this

public class RootObject
{
    public string key1 { get; set; }
    public List<string> fileName { get; set; }
}

and change the Dictionary<String, List<String>> to RootObject

var values = JsonConvert.DeserializeObject<RootObject>(json);

Just reiterating @phantasm's answer:

The JSON is incorrect. It is missing a comma. Below is the correct JSON.

{
    "key1" : "value1",
    "fileName" : [
        "filename1.docx",
        "filename2.xlsx"
    ]
}

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