Json to Dictionary

Jun 6, 2011 at 3:26 PM


I would like to use

�JsonConvert.DeserializeObject<Dictionary<string, object>>
to create a dictionary from Json.

The other way around

(JsonConvert.SerializeObject(jsonData, Formatting.Indented))

it worked fine.


This is my Json:


d.data is the first key of my dictionary with a value of a new dictionary.

Unfortunately JsonConverter just adds the value object to the key d.data and does not create a new dictionary.

Is there a way to tell Json Converter to create a new dictionary inside my dictionary for each object?




Jun 6, 2011 at 3:51 PM

I'm not sure that this is exactly what you are looking for, but the following might be one way to have the desired result:

var results = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonData);
var tokenList = (IEnumerable<JToken>)(results["d.data"]);
var dataDictionary = new Dictionary<string, object>();

foreach (JToken t in tokenList)
      int plateNum = t["PlateNumber"];
      dataDictionary.Add("PlateNumber", plateNum);

Could this possibly be a solution for you? Let me know if it works!

Jun 7, 2011 at 6:49 AM
Edited Jun 7, 2011 at 6:49 AM

Thanks for your answer.

Maybe my goal was not clear enough. Probably because english is not my first language.

Here is the dictionary I would like to create:


It should be dynamic so I can create a Dictionary out of this given Json string


{"d.data":{"Name":"New Organization","AlternateIdentificationsActions":{"update":null,"destroy":null,"create":{"d.data":{"Name":"Another name for it","Id":"db0ec630-59c8-4b75-bab5-9efb008ff6bf"}}},"Id":"ad37be5a-3f65-4278-9acb-9efb008ff6aa"}}



I hope this makes it a bit clearer.

Thanks again for your help.