反序列化JSON用于Windows Phone的 [英] JSON Deserializing for Windows Phone

查看:135
本文介绍了反序列化JSON用于Windows Phone的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想反序列化JSON之后,但我真的不知道如何使用JSON.net做的工作。我使用C#和JSON.Net库



我的JSON如下:

  {
发现:3,
界限:[
[
-43.54919,
172.62148
],
[
-43.54487,
172.63654
]
],
特点:[
{
标识:15342454,
心:{
类型:POINT,
坐标:[
-43.54779,
172.62148
]
},
界限:[
[
-43.54779,
172.62148
],
[
-43.54779,
172.62148
]
],
特性:{
osm_element:节点,
美化市容:厕所,
synthesized_name:厕所,
osm_id:502884303
},
几何体:{
类型:POINT,
坐标:
-43.54779,
172.62148
]
},
位置:{
县志:坎特伯雷,
国:新西兰,
路:萨默西特新月,
城市:基督城
},
类型: 功能
},
{
标识:19313858,
心:{
类型:POINT,
坐标:[
-43.54919,
172.63654
]
},
界限:[
[
-43.54919,
172.63654
],
[
-43.54919,
172.63654
]
],
特性:{
osm_element:节点,
美化市容:厕所,
synthesized_name:厕所,
osm_id:676225633
},
几何体:{
类型:POINT,
坐标:[
-43.54919,
172.63654
]
}
位置:{
县志:坎特伯雷,
国:新西兰,
路:科伦坡街,
城市:基督城
},
类型:功能
},
{
标识:22536275,
心:{
类型:POINT,
坐标:[
-43.54487,
172.63632
]
} ,
界限:[
[
-43.54487,
172.63632
],
[
-43.54487,
172.63632
]
],
特性:{
osm_element:节点,
美化市容:厕所,
synthesized_name :厕所,
osm_id:864392689
},
几何体:{
类型:POINT,
坐标:
-43.54487,
172.63632
]
},
位置:{
县志:坎特伯雷,
国:新西兰,
路:华兹华斯街,
城市:基督城
},
类型:功能
}
],
类型:FeatureCollection,
CRS:{
类型:EPSG,
属性:{
代码:4326,
coordinate_order:[
0,
1
]
}
}
}


解决方案

您不必申报许多微小的类反序列化。只要使用动态的。
以下是一个例子。



 字符串jsonstr = @{发现:3,界:[-43.54919,172.62148],[-43.54487,172.63654],功能:[{ID:15342454,质心:{类型: POINT,,坐标:[ -  43.54779,172.62148]},界:[-43.54779,172.62148],[-43.54779,172.62148],属性中:{osm_element :节点,,美化市容:厕所,synthesized_name:厕所,osm_id:502884303},几何 :{类型:点,协调:[ -  43.54779,172.62148]},位置:{县:坎特伯雷,国家:新西兰,道:萨默西特新月,城市:基督城},类型:功能}, {ID:19313858,质心:{类型:点,协调:[ -  43.54919,172.63654]},界:[ -43.54919,172.63654],[-43.54919,172.63654],属性中:{osm_element:节点,,美化市容:厕所,synthesized_name :厕所,osm_id:676225633},几何:{类型:点,协调:[ -  43.54919,172.63654 ]},位置:{县:坎特伯雷,国家:新西兰,道:科伦坡街,城市:基督城},类型:功能},{ID:22536275,质心:{类型:POINT ,协调:[ -  43.54487,172.63632]},界:[-43.54487,172.63632],[-43.54487,172.63632],属性中:{osm_element :节点,,美化市容:厕所,synthesized_name:厕所,osm_id:864392689},几何: {类型:点,协调:[ -  43.54487,172.63632]},位置:{县:坎特伯雷,国家 :新西兰,道:华兹华斯街,城市:基督城},类型:功能}], 类型,:,FeatureCollection,CRS:{类型,:,EPSG,属性,:{代码:4326,coordinate_order :[0,1]}}}; 

动态JSON = JsonConvert.DeserializeObject(jsonstr);
的foreach(在json.features VAR功能)
{
Console.Write({0},{1} - {2},{3},
功能.bounds [0] [0],feature.bounds [0] [1],
feature.bounds [1] [0],feature.bounds [1] [1]);

Console.WriteLine({0} {1} {2} {3},
feature.location.country,feature.location.county,feature.location.city,功能.location.road);
}



非动态版本

  JObject JSON =(JObject)JsonConvert.DeserializeObject(jsonstr); 
的foreach(在JSON VAR功能[功能])
{
Console.Write({0},{1} - {2},{3},
功能[边界] [0] [0],功能[边界] [0] [1],
功能[边界] [1] [0],功能[界限 ] [1] [1]);
Console.WriteLine({0} {1} {2} {3},
功能[所在地] [国],功能[所在地] [县] ,功能[所在地] [市],功能[所在地] [路]);
}


I am trying to deserialize the following JSON, but I really have no idea how to use JSON.net to do the work. I am using C# and JSON.Net library.

My JSON is as follows:

{
    "found": 3,
    "bounds": [
        [
            -43.54919,
            172.62148
        ],
        [
            -43.54487,
            172.63654
        ]
    ],
    "features": [
        {
            "id": 15342454,
            "centroid": {
                "type": "POINT",
                "coordinates": [
                    -43.54779,
                    172.62148
                ]
            },
            "bounds": [
                [
                    -43.54779,
                    172.62148
                ],
                [
                    -43.54779,
                    172.62148
                ]
            ],
            "properties": {
                "osm_element": "node",
                "amenity": "toilets",
                "synthesized_name": "Toilets",
                "osm_id": "502884303"
            },
            "geometry": {
                "type": "POINT",
                "coordinates": [
                    -43.54779,
                    172.62148
                ]
            },
            "location": {
                "county": "Canterbury",
                "country": "New Zealand",
                "road": "Sommerset Crescent",
                "city": "Christchurch"
            },
            "type": "Feature"
        },
        {
            "id": 19313858,
            "centroid": {
                "type": "POINT",
                "coordinates": [
                    -43.54919,
                    172.63654
                ]
            },
            "bounds": [
                [
                    -43.54919,
                    172.63654
                ],
                [
                    -43.54919,
                    172.63654
                ]
            ],
            "properties": {
                "osm_element": "node",
                "amenity": "toilets",
                "synthesized_name": "Toilets",
                "osm_id": "676225633"
            },
            "geometry": {
                "type": "POINT",
                "coordinates": [
                    -43.54919,
                    172.63654
                ]
            },
            "location": {
                "county": "Canterbury",
                "country": "New Zealand",
                "road": "Colombo Street",
                "city": "Christchurch"
            },
            "type": "Feature"
        },
        {
            "id": 22536275,
            "centroid": {
                "type": "POINT",
                "coordinates": [
                    -43.54487,
                    172.63632
                ]
            },
            "bounds": [
                [
                    -43.54487,
                    172.63632
                ],
                [
                    -43.54487,
                    172.63632
                ]
            ],
            "properties": {
                "osm_element": "node",
                "amenity": "toilets",
                "synthesized_name": "Toilets",
                "osm_id": "864392689"
            },
            "geometry": {
                "type": "POINT",
                "coordinates": [
                    -43.54487,
                    172.63632
                ]
            },
            "location": {
                "county": "Canterbury",
                "country": "New Zealand",
                "road": "Wordsworth Street",
                "city": "Christchurch"
            },
            "type": "Feature"
        }
    ],
    "type": "FeatureCollection",
    "crs": {
        "type": "EPSG",
        "properties": {
            "code": 4326,
            "coordinate_order": [
                0,
                1
            ]
        }
    }
}

解决方案

You don't have to declare many tiny classes to deserialize. Just make use of dynamic. Here is a working example

string jsonstr = @"{""found"": 3, ""bounds"": [[-43.54919, 172.62148], [-43.54487, 172.63654]], ""features"": [{""id"": 15342454,""centroid"": {""type"":""POINT"",""coordinates"":[-43.54779, 172.62148]},""bounds"": [[-43.54779, 172.62148], [-43.54779, 172.62148]],""properties"": {""osm_element"": ""node"", ""amenity"": ""toilets"", ""synthesized_name"": ""Toilets"", ""osm_id"": ""502884303""},""geometry"": {""type"":""POINT"",""coordinates"":[-43.54779, 172.62148]},""location"": {""county"": ""Canterbury"", ""country"": ""New Zealand"", ""road"": ""Sommerset Crescent"", ""city"": ""Christchurch""},""type"": ""Feature""},{""id"": 19313858,""centroid"": {""type"":""POINT"",""coordinates"":[-43.54919, 172.63654]},""bounds"": [[-43.54919, 172.63654], [-43.54919, 172.63654]],""properties"": {""osm_element"": ""node"", ""amenity"": ""toilets"", ""synthesized_name"": ""Toilets"", ""osm_id"": ""676225633""},""geometry"": {""type"":""POINT"",""coordinates"":[-43.54919, 172.63654]},""location"": {""county"": ""Canterbury"", ""country"": ""New Zealand"", ""road"": ""Colombo Street"", ""city"": ""Christchurch""},""type"": ""Feature""},{""id"": 22536275,""centroid"": {""type"":""POINT"",""coordinates"":[-43.54487, 172.63632]},""bounds"": [[-43.54487, 172.63632], [-43.54487, 172.63632]],""properties"": {""osm_element"": ""node"", ""amenity"": ""toilets"", ""synthesized_name"": ""Toilets"", ""osm_id"": ""864392689""},""geometry"": {""type"":""POINT"",""coordinates"":[-43.54487, 172.63632]},""location"": {""county"": ""Canterbury"", ""country"": ""New Zealand"", ""road"": ""Wordsworth Street"", ""city"": ""Christchurch""},""type"": ""Feature""}], ""type"": ""FeatureCollection"", ""crs"": {""type"": ""EPSG"", ""properties"": {""code"": 4326, ""coordinate_order"": [0, 1]}}}";

dynamic json = JsonConvert.DeserializeObject(jsonstr);
foreach (var feature in json.features)
{
    Console.Write("{0},{1} - {2},{3} : ", 
        feature.bounds[0][0], feature.bounds[0][1], 
        feature.bounds[1][0], feature.bounds[1][1]);

    Console.WriteLine("{0} {1} {2} {3}", 
        feature.location.country, feature.location.county, feature.location.city, feature.location.road);
}

Non-dynamic version

JObject json = (JObject)JsonConvert.DeserializeObject(jsonstr);
foreach (var feature in json["features"])
{
    Console.Write("{0},{1} - {2},{3} : ", 
        feature["bounds"][0][0], feature["bounds"][0][1], 
        feature["bounds"][1][0], feature["bounds"][1][1]);
    Console.WriteLine("{0} {1} {2} {3}",
        feature["location"]["country"], feature["location"]["county"], feature["location"]["city"], feature["location"]["road"]);
}

这篇关于反序列化JSON用于Windows Phone的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆