[英]How to convert JSON string with different value types to C# Dictionary?
So, I want to convert this JSON string: 因此,我要转换此JSON字符串:
{
"Posts" :
[
{
"Title" : "just a string",
"Id" : "231"
}, {
"Title" : "Another string as title",
"Id": "41"
}
],
"anotherKey" : "third string"
}
to C# Dictionary, but this code (using Newtonsoft.Json
package; where jsonString
is variable storing that JSON string): 到C#字典,但是此代码(使用
Newtonsoft.Json
包;其中jsonString
是存储该JSON字符串的变量):
Dictionary<string,Array> test1 = JsonConvert.DeserializeObject<Dictionary<string,Array>>(jsonString);
Console.WriteLine(test1.Count);
returns an error: 返回错误:
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.
Is another way how to convert this string to Dictionary? 是另一种方法如何将此字符串转换为Dictionary?
The second entry isn't an array, it's a single string. 第二个条目不是数组,而是单个字符串。
Try deserializing to Dictionary<string,object>
or Dictionary<string,dynamic>
to read all values. 尝试反序列化为
Dictionary<string,object>
或Dictionary<string,dynamic>
以读取所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.