I'm using JSON.NET to parse a JSON reponse from openexhangerates.org server side using .NET. The response contains a nested object ("rates") which has a long list of numeric properties:
{
"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...
}
}
The property names correspond to the currency type (e.g. "USD"). I need to assume that the list of properties can change over time, so I want to convert the object into a Dictionary instead of a corresponding C# object.
So instead of deserializing the JSON object into something like this:
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...
}
I want to end up with this:
Dictionary<string,decimal>() {{"AED",0.2828},{"AFN",0.3373},{"ALL",2.2823},{"AMD",33.378} // etc...};
How do I do this starting from either the response string or from the JObject produced by calling JObject.Parse(responseString)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…