0
所以,我想这JSON字符串转换:如何将具有不同值类型的JSON字符串转换为C#字典?
{
"Posts" :
[
{
"Title" : "just a string",
"Id" : "231"
}, {
"Title" : "Another string as title",
"Id": "41"
}
],
"anotherKey" : "third string"
}
到C#字典,但该代码(使用Newtonsoft.Json
包;其中jsonString
是可变的存储那个JSON字符串):
Dictionary<string,Array> test1 = JsonConvert.DeserializeObject<Dictionary<string,Array>>(jsonString);
Console.WriteLine(test1.Count);
返回一个错误:
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Unable to find a constructor to use for type System.Array. Path 'Posts', line 1, position 10.
是另一种方式如何将此字符串转换为Dictionary?
第二项不是数组,它是单个字符串。如果你真的想将单个对象与集合或者Dictionary'混合使用,试试对'Dictionary 进行反序列化。 –
不是我想要混合它们,但它是来自我未编码的API的JSON字符串的一部分。但'字典<字符串,动态>'的作品,所以谢谢你:) –