JSON Deserializing for Windows Phone

隐身守侯 提交于 2019-12-20 06:23:57

问题


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
            ]
        }
    }
}

回答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"]);
}



回答2:


public class Centroid
{
    public string type { get; set; }
    public List<double> coordinates { get; set; }
}

public class Properties
{
    public string osm_element { get; set; }
    public string amenity { get; set; }
    public string synthesized_name { get; set; }
    public string osm_id { get; set; }
}

public class Geometry
{
    public string type { get; set; }
    public List<double> coordinates { get; set; }
}

public class Location
{
    public string county { get; set; }
    public string country { get; set; }
    public string road { get; set; }
    public string city { get; set; }
}

public class Feature
{
    public int id { get; set; }
    public Centroid centroid { get; set; }
    public List<List<double>> bounds { get; set; }
    public Properties properties { get; set; }
    public Geometry geometry { get; set; }
    public Location location { get; set; }
    public string type { get; set; }
}

public class Properties2
{
    public int code { get; set; }
    public List<int> coordinate_order { get; set; }
}

public class Crs
{
    public string type { get; set; }
    public Properties2 properties { get; set; }
}

public class RootObject
{
    public int found { get; set; }
    public List<List<double>> bounds { get; set; }
    public List<Feature> features { get; set; }
    public string type { get; set; }
    public Crs crs { get; set; }
}

Here you go.

There is a tool for generating C# classes from json http://json2csharp.com/.




回答3:


First create a class that fits the JSONed object.
Then, simply write JsonConvert.DeserializeObject<ClassName>(json)
Where ClassName is the name of your class and json is a string containing you JSON.

You have quite a complex data structure, so creating a class for it might be a bit complex. You might want to maybe simplify it a little bit.




回答4:


How I would tackle this....

First take your JSON and paste it into: http://jsonviewer.stack.hu/ - this gives you a view like:

Now, from the objects shown in that view create a class for each type of object - e.g:

public class MainWrapper
{
    public int found {get;set;}
    public List<Bound> bounds {get;set;}
    public List<Feature> features {get;set;}
    public Crs crs {get;set;
}

Finally you can now use some Newtonsoft to deserialize as: JsonConvert.DeserializeObject<MainWrapper>(text)



来源:https://stackoverflow.com/questions/9680631/json-deserializing-for-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!