【发布时间】:2012-04-24 07:14:38
【问题描述】:
今天下午我花了很长时间尝试在字符串中实现 JSON 的反序列化,起初我使用 DataContractJsonSerializer,因为我的环境是 Silverlight,但它似乎不支持使用开箱即用的字典(在许多其他 SO 问题中提出)。
作为替代方案,我决定暂时使用 JSON.NET(基于上述 SO 问题的答案),我遇到了以下问题。
我想反序列化下面的 JSON:
{
"disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability or fitness for any purpose; use at your own risk. Other than that - have fun, and please share/watch/fork if you think data like this should be free!",
"license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.",
"timestamp": 1334183999,
"base": "USD",
"rates": {
"AED": 3.6732,
"AFN": 48.400002,
"ALL": 106.669998,
}
}
并将其放在以下对象中(字典中的双精度是必需的):
public class ExchangeData
{
public string disclaimer { get; set; }
public string license { get; set; }
public string timestamp { get; set; }
public string @base { get; set; }
public Dictionary<string, double> rates { get; set; }
}
我最近的实际尝试如下:
StreamReader reader = new StreamReader(args.Result);
ExchangeData data = JsonConvert.DeserializeObject<ExchangeData>(reader.ReadToEnd());
但这会导致以下异常:
无法从程序集“System.Core, Version=3.7.0.0, Culture=neutral, PublicKeyToken=969DB8053D3322AC”加载类型“System.Dynamic.IDynamicMetaObjectProvider”。
根据你所看到的,我的方法完全错误,还是我只是犯了一个小学生的错误(或两者兼而有之!)
感谢您的宝贵时间!
【问题讨论】:
标签: c# json silverlight-4.0 dictionary json.net