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 nume
JObject
already implements IDictionary
, so I suspect that when you've navigated down to the rates
member, you should be able to use:
var result = rates.ToDictionary(pair => pair.Key, pair => (decimal) pair.Value);
Unfortunately it uses explicit interface implementation, which makes this a bit of a pain - but if you go via the IDictionary
interface, it's fine.
Here's a short but complete example which appears to work with the JSON you've provided (saved into a test.json
file):
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
class Test
{
static void Main()
{
JObject parsed = JObject.Parse(File.ReadAllText("test.json"));
IDictionary rates = (JObject) parsed["rates"];
// Explicit typing just for "proof" here
Dictionary dictionary =
rates.ToDictionary(pair => pair.Key,
pair => (decimal) pair.Value);
Console.WriteLine(dictionary["ALL"]);
}
}