【问题标题】:How to deserialize json with nested Dictionaries?如何使用嵌套字典反序列化 json?
【发布时间】:2011-06-03 17:10:52
【问题描述】:

对于某些端点,SimpleGeo.com 返回如下内容:

{
    "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/SG_5JkVsYK82eLj26eomFrI7S_37.795027_-122.421583@1291796505.json,
        "address":"1900 Polk St",
        "owner":"simplegeo",
        "postcode":"94109",
        "classifiers":[
            {
                "category":"Restaurant",
                "type":"Food & Drink",
                "subcategory":""                  
            }             
        ]          
    }     
}

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

现在我在反序列化“属性”部分时遇到了一个小问题。如果我使用例如一种 Dictionary 类型,它将其转换为一个不错的字典,但 'classifiers' 值只是一个 {} 字符串。

有没有办法告诉 json.net 将子数组反序列化为另一个 Dictionary 等?基本上,该返回中有一定数量的普通键/值,但我知道可能不仅仅是“分类器”子数组(参见“标签”),而且值的深度可能更进一步。 ..

所以基本上我想知道的是,如何正确反序列化属性部分?有什么建议?我不介意编写自己的 JsonConverter,但也许已经有一种方法可以不用它。

【问题讨论】:

    标签: json parsing json.net deserialization


    【解决方案1】:

    我在这里找到了类似问题的解决方案: Json.NET: Deserializing nested dictionaries.

    它使用自定义的 JsonConverter,没有它我看不出有什么办法。

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多