Silverlight 使用字典反序列化复杂json对象
我正在使用Newtonsoft Json.Net将Json提要反序列化到对象中: Json: C级:Silverlight 使用字典反序列化复杂json对象,silverlight,c#-4.0,json.net,deserialization,Silverlight,C# 4.0,Json.net,Deserialization,我正在使用Newtonsoft Json.Net将Json提要反序列化到对象中: Json: C级: 公共类MyClassName { 公共字符串staticKey1{get;set;} 公共字符串staticKey2{get;set;} 公共字典结果{get;set;} } 我使用的是Newtonsoft.Json.JsonConvert.DeserializeObject(),但出现异常: 无法将当前JSON数组(例如[1,2,3])反序列化为类型 'System.Collections.
公共类MyClassName
{
公共字符串staticKey1{get;set;}
公共字符串staticKey2{get;set;}
公共字典结果{get;set;}
}
我使用的是Newtonsoft.Json.JsonConvert.DeserializeObject(),但出现异常:
无法将当前JSON数组(例如[1,2,3])反序列化为类型
'System.Collections.Generic.Dictionary'2[System.String,System.String]'
因为该类型需要一个JSON对象(例如{“name”:“value”})来
正确地反序列化。要修复此错误,请将JSON更改为
JSON对象(例如{“name”:“value”})或将反序列化类型更改为
实现集合接口的数组或类型(例如。
可以从JSON反序列化的类似ICollection(IList)的列表
数组。还可以将JsonArrayAttribute添加到类型中,以强制它
从JSON数组反序列化
实际上,它很容易使用:
public class MyClass
{
public string staticKey1 { get; set; }
public string staticKey2 { get; set; }
public IEnumerable<IDictionary<string, string>> results { get; set; }
}
公共类MyClass
{
公共字符串staticKey1{get;set;}
公共字符串staticKey2{get;set;}
公共IEnumerable结果{get;set;}
}
但也许有更好的解决办法
public class MyClassName
{
public string staticKey1 { get; set; }
public string staticKey2 { get; set; }
public Dictionary<String, String> results { get; set; }
}
public class MyClass
{
public string staticKey1 { get; set; }
public string staticKey2 { get; set; }
public IEnumerable<IDictionary<string, string>> results { get; set; }
}