简体   繁体   中英

How I can deserialize Json into Dictionary<String,List<String>>

I have a sample Json

[{"_2":["HR Data","Reformed (Master File)"]}]

and I am trying to deserialize it into below model

public class ExploreCriteria
    {
        public Dictionary<String, List<String>> Explore { get; set; }
    }

this is what I have tried so far

ExploreCriteria Explore = new ExploreCriteria();
Explore = JsonConvert.DeserializeObject<ExploreCriteria>(JsonStr);

but it says

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'DataModels.ExploreCriteria' because the type requires a JSON object
(e.g. {"name":"value"}) to deserialize correctly.

The provided JSON and your ExploreCriteria class do not describe the same structure.

Your JSON structure is an array that contains a key with an array value. So you can either remove the square brackets to

{"_2":["HR Data","Reformed (Master File)"]}

then your ExploreCriteria is fitting. Or you can change the JsonConvert call to

var JsonStr = "[{\"_2\":[\"HR Data\",\"Reformed(Master File)\"]}]";
ExploreCriteria Explore = new ExploreCriteria();
var data = JsonConvert.DeserializeObject<IEnumerable<Dictionary<String, List<string>>>>(JsonStr);
Explore.Explore = data.FirstOrDefault();

List<KeyValuePair<string, List<string>>> uploadedfiles = JsonConvert.DeserializeObject<List<KeyValuePair<string, List<string>>>>(json);

使用keyvaluepair类而不是字典。

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