Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize json with nested Dictionaries?

For some endpoints SimpleGeo.com returns something like this:

{
    "geometry":{
        "type":"Point",
        "coordinates":[
            -122.421583,
            37.795027    
        ]          
    },
    "type":"Feature",
    "id":SG_5JkVsYK82eLj26eomFrI7S_37.795027_-122.421583@1291796505,
    "properties":{
        "province":"CA",
        "city":"San Francisco",
        "name":"Bell Tower",
        "tags":[],
        "country":"US",
        "phone":"+1 415 567 9596",
        "href": http://api.simplegeo.com/1.0/features/[email protected],
        "address":"1900 Polk St",
        "owner":"simplegeo",
        "postcode":"94109",
        "classifiers":[
            {
                "category":"Restaurant",
                "type":"Food & Drink",
                "subcategory":""                  
            }             
        ]          
    }     
}

(see http://simplegeo.com/docs/api-endpoints/simplegeo-features#get-detailed-information).

Now I have a small problem deserializing the 'properties' part. If I use e.g. a type of Dictionary it converts it to a nice dictionary, but the 'classifiers' value is just one {} string.

Is there any way to tell json.net to deserialize sub-arrays into yet another Dictionary etc etc? Basically there's an amount of plain key/values in that return, but I do know that there might be more than just that 'classifiers' sub-array (see the 'tags'), and maybe the depth goes even further in the values...

So basically what I was wondering is, how do I properly deserialize the properties part? Any suggestions? I don't mind writing my own JsonConverter, but maybe there is already a way that works without it.

like image 372
Jörg Battermann Avatar asked Jan 04 '11 23:01

Jörg Battermann


1 Answers

I've found a solution for a similar question over here: Json.NET: Deserializing nested dictionaries.

It uses a custom JsonConverter and I don't see a way to do without it.

like image 155
AlexD Avatar answered Nov 09 '22 20:11

AlexD