Json to Dictionary

Jun 6, 2011 at 4:26 PM

Hi,

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":{"PlateNumber":"2342423423","FabricationDate":null,"Type":null,"Model":null,"Colour":null,"Comments":null,"Id":"57bfa9bc-af28-4b47-bd5b-9efa011d64e3"}}

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?

Thanks,

 

Sascha

Jun 6, 2011 at 4: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 7:49 AM
Edited Jun 7, 2011 at 7: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:

http://www.flickr.com/photos/63771562@N07/5807612522/in/photostream/

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"}}

 

http://www.flickr.com/photos/63771562@N07/5807065489/in/photostream

I hope this makes it a bit clearer.

Thanks again for your help.