Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET: Deserializing part of a JSON object to a dictionary

Tags:

.net

json.net

I have JSON like this:

{
   "Property":"Blah blah",
   "Dictionary": {
        "Key1" : "Value1",
        "Key2" : "Value2",
        "Key3" : "Value3"
   }
}

I want to extract the "Dictionary" object as a Dictionary (so it'd be like Key1 => Value1, etc.). If I just had the "Dictionary" object directly, I could use:

 JsonConvert.DeserializeObject<Dictionary<string, string>>

What's the best way to get just the Dictionary property as a Dictionary?

Thanks in advance! Tim

like image 353
Tim Ridgely Avatar asked Oct 27 '10 16:10

Tim Ridgely


1 Answers

Took me a little while to figure out, but I just didn't feel great about using string parsing or regexes to get at the inner JSON that I want.

Simple enough; I did something along these lines to get at the inner data:

var jObj = JObject.Parse(jsonText);
var innerJObj = JObject.FromObject(jObj["Dictionary"]);

Works well enough.

like image 61
Tim Ridgely Avatar answered Oct 11 '22 12:10

Tim Ridgely