简体   繁体   English

试图将 JSON 反序列化为字典<string, list<string> &gt; 在 c#</string,>

[英]Trying to deserialize JSON into Dictionary<string, List<string>> in c#

I'm trying to deserialize JSON into a dictionary with strings as keys and lists of strings as values.我正在尝试将 JSON 反序列化为一个字典,其中字符串作为键,字符串列表作为值。 The way I did it is this:我这样做的方式是这样的:

[
    {
        "key1": [
            "value1.1",
            "value1.2"
        ]
    },
    {
        "key2": [
            "value2.1",
            "value2.2"
        ]
    },
]

and to deserialize:并反序列化:

Dictionary<string, List<string>> dictionary;
string text = File.ReadAllText(@"Text.json");
dictionary = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(text);

I am receiving the errors:我收到错误:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary 2[System.String,System.Collections.Generic.List 1[System.String]]' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary 2[System.String,System.Collections.Generic.List 1[System. String]]' 因为该类型需要 JSON object(例如 {"name":"value"})才能正确反序列化。
To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array.要修复此错误,请将 JSON 更改为 JSON object(例如 {"name":"value"})或将实现的反序列化类型接口更改为数组或数组的 ILists 接口(IColle 之类的类型)从 JSON 阵列反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。 Path '', line 1, position 1.'路径'',第 1 行,position 1。

Does anyone have have any insights for me?有人对我有任何见解吗?

Solution As Per Question:根据问题的解决方案:

Try this, Hopefully this will help you.试试这个,希望这会对你有所帮助。 https://dotnetfiddle.net/f3u1TC https://dotnetfiddle.net/f3u1TC

string json = @"[{""key1"":[""value1.1"",""value1.2""]},{""key2"":[""value2.1"",""value2.2""]}]";
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, List<string>>[]>(json);

Edit编辑

Updated Solution:更新的解决方案:

If you don't need an array.如果你不需要数组。 Then you have to update your json.然后你必须更新你的 json。 Remove array braces [] and add these ones {}删除数组大括号[]并添加这些{}

Json Json

{
    {
        "key1": [
            "value1.1",
            "value1.2"
        ]
    },
    {
        "key2": [
            "value2.1",
            "value2.2"
        ]
    },
}

C# C#

string json = @"{""key1"":[""value1.1"",""value1.2""],""key2"":[""value2.1"",""value2.2""]}";
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(json);

JSON syntax for dictionary is { "key1": "val1", "key2: "val2".....}字典的 JSON 语法是{ "key1": "val1", "key2: "val2".....}

So if your JSON is structured as follows, your conversion will work as it is.因此,如果您的 JSON 的结构如下,您的转换将按原样进行。

string json = @"{""key1"":[""value1.1"",""value1.2""],""key2"":[""value2.1"",""value2.2""]}";
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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