如何使用嵌套字典反序列化 json?

发布于 2024-10-10 01:47:19 字数 1564 浏览 0 评论 0原文

对于某些端点 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/[email protected],
        "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)。

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

有什么方法可以告诉 json.net 将子数组反序列化到另一个字典等中吗?基本上,该返回中有大量简单的键/值,但我确实知道可能不仅仅是“分类器”子数组(请参阅“标签”),而且值的深度可能更进一步。 ..

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-10-17 01:47:19

我在这里找到了类似问题的解决方案:
Json.NET:反序列化嵌套字典

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文