簡體   English   中英

我如何將Json反序列化為Dictionary <String,List<String> &gt;

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

我有一個樣本詹森

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

我正在嘗試將其反序列化為以下模型

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

這是我到目前為止嘗試過的

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

但它說

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.

提供的JSON和您的ExploreCriteria類沒有描述相同的結構。

您的JSON結構是一個數組,其中包含帶有數組值的鍵。 因此,您可以刪除方括號以

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

那么您的ExploreCriteria很合適。 或者您可以將JsonConvert調用更改為

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類而不是字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM