【发布时间】:2013-01-04 17:11:58
【问题描述】:
我正在使用 JSON.NET 来解析来自 openexhangerates.org 服务器端的 JSON 响应。响应包含一个嵌套对象(“rates”),其中包含一长串数字属性:
{
"disclaimer": "Exchange rates provided 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! Usage subject to acceptance of terms: http://openexchangerates.org/terms/",
"license": "Data sourced from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Usage subject to acceptance of license agreement: http://openexchangerates.org/license/",
"timestamp": 1357268408,
"base": "USD",
"rates": {
"AED": 3.673033,
"AFN": 51.5663,
"ALL": 106.813749,
"AMD": 403.579996,
etc...
}
}
属性名称对应于货币类型(例如“USD”)。我需要假设属性列表会随着时间而改变,所以我想将对象转换为 Dictionary 而不是对应的 C# 对象。
所以不要将 JSON 对象反序列化成这样的东西:
class Rates
{
public decimal AED; // United Arab Emirates Dirham
public decimal AFN; // Afghan Afghani
public decimal ALL; // Albanian Lek
public decimal AMD; // Armenian Dram
// etc...
}
我想这样结束:
Dictionary<string,decimal>() {{"AED",0.2828},{"AFN",0.3373},{"ALL",2.2823},{"AMD",33.378} // etc...};
如何从响应字符串或调用 JObject.Parse(responseString) 生成的 JObject 开始?
【问题讨论】:
-
你有没有看过谷歌搜索结果关于如何使用 C# 反序列化 JSON 互联网上有很多例子..stackoverflow.com/questions/6375122/…