![](/img/trans.png)
[英]Can not deserialize json to Dictionary<string, List<Purchase>>
[英]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.