我使用JSON.NET從openexhangerates.org服務器端使用.NET解析JSON響應。該響應包含嵌套的對象(「速率」),其具有數值屬性一長串:將JSON.NET JObject的屬性/標記轉換爲字典鍵
{
"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調用JObject.Parse(responseString)生成?
你看過谷歌搜索結果如何反序列化使用C#的JSON有很多互聯網上的例子.. http://stackoverflow.com/questions/6375122/how-to-parse-json-response-進入詞典 – MethodMan