简体   繁体   中英

JSON Deserializing for Windows Phone

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"]);
}
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/ .

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.

How I would tackle this....

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

JSON格式

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM