I am sort of new to C# programming and am having trouble with the dictionary using .net 4.0.
I have sent a JSON object through JavaScriptSerializer into a Dictionary<string, object> object which worked great at extracting all the data.
JSON chain
{
"name" : "MrMonkey",
"type" : "monkey",
"location" : {
"id" : "125235",
"name" : "zoo"
},
"owner" : {
"id" : "4211",
"name" : "Biggles"
}
}
In this dictionary object created I have daughter levels that store information I need to extract from the dictionary and store elsewhere. Say I want is to extract the location name. As you can see it also shares a keyname with the parent level and another daughter level.
For the parent level I can extract information as simply as contact.name = dict["name"].ToString(); but how would I go about extracting the required information from the daughter levels?
I was able to create a work around in JSON.net to get this to work with a little bit of fiddling by checking the datatype and then converting it if it fell within a certain type, but this was aggravating and I been told by the boss not to use JSON.net.
Without having tried, I’d try something like:
(dict["location"] as Dictionary<string,object>)["name"], as I’d presume from the JSON you’ve provided, that the daughters themselves are again deserialized intoDictionarys.Anyway, the debugger will help you a lot here. If you set a breakpoint on the line after the call to the deserialization, you can inspect your dictionary (point the mouse to it and wait a second) and have a look at how your structure is now stored in C# objects.