我正在尝试将形如
[{"key" : "Microsoft", "value":[{"Key":"Publisher","Value":"abc"},{"Key":"UninstallString","Value":"c:\temp"}]} and so on ]
的json字符串反序列化为C#对象。它基本上是一个 Dicionary<string, Dictionary<string, string>>
的形式。我尝试使用Newtonsoft的JsonConvert.Deserialize
,但是出现了错误:
无法将当前JSON数组(例如[1,2,3])反序列化为类型'System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.Dictionary`2[System.String,System.String]]',因为该类型需要一个JSON对象(例如{"name":"value"})才能正确反序列化。是否有其他替代方法?
要修复此错误,请将JSON更改为JSON对象(例如{"name":"value"}),或将反序列化类型更改为数组或实现集合接口(例如ICollection、IList)的类型,如可从JSON数组反序列化的List。也可以向类型添加JsonArrayAttribute,以强制其从JSON数组反序列化。 路径“”,行1,位置1。
var obj = JsonConvert.DeserializeObject(...)
。它适用于您的 JSON 字符串。 - L.B