JSON反序列化Windows Phone

时间:2012-03-13 08:36:49

标签: c# json windows-phone-7 windows-phone-7.1 deserialization

我正在尝试反序列化以下JSON,但我真的不知道如何使用JSON.net来完成这项工作。我正在使用C#和JSON.Net库。

我的JSON如下:

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

4 个答案:

答案 0 :(得分:6)

您不必声明许多小类来反序列化。只需使用dynamic即可。 这是一个工作示例

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);
}

非动态版

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

答案 1 :(得分:3)

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

你走了。

有一个工具可以从json http://json2csharp.com/生成C#类。

答案 2 :(得分:1)

首先创建一个适合JSONed对象的类 然后,只需写下JsonConvert.DeserializeObject<ClassName>(json)
其中ClassName是您的类的名称,json是包含JSON的字符串。

你有一个非常复杂的数据结构,所以为它创建一个类可能有点复杂。 您可能希望稍微简化一下。

答案 3 :(得分:1)

我将如何解决这个问题....

首先获取您的JSON并将其粘贴到:http://jsonviewer.stack.hu/ - 这会为您提供如下视图:

JSON

现在,从该视图中显示的对象为每种类型的对象创建一个类 - 例如:

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

最后,您现在可以使用一些Newtonsoft反序列化为:JsonConvert.DeserializeObject<MainWrapper>(text)