Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 针对Windows Phone的JSON反序列化_C#_Json_Windows Phone 7_Windows Phone 7.1_Deserialization - Fatal编程技术网

C# 针对Windows Phone的JSON反序列化

C# 针对Windows Phone的JSON反序列化,c#,json,windows-phone-7,windows-phone-7.1,deserialization,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 ] ], "f

我正在尝试反序列化下面的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
            ]
        }
    }
}

首先创建一个适合JSONed对象的类。
然后,只需编写
JsonConvert.DeserializeObject(json)

其中,
ClassName
是类的名称,
json
是包含json的字符串

您有一个相当复杂的数据结构,因此为它创建一个类可能有点复杂。
您可能希望将其简化一点。

您不必声明许多小类来进行反序列化。只要利用
动态
。 下面是一个工作示例

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"]);
}
公共类质心
{
公共字符串类型{get;set;}
公共列表坐标{get;set;}
}
公共类属性
{
公共字符串osm_元素{get;set;}
公共字符串{get;set;}
公共字符串名称{get;set;}
公共字符串osm_id{get;set;}
}
公共课几何
{
公共字符串类型{get;set;}
公共列表坐标{get;set;}
}
公共类位置
{
公共字符串country{get;set;}
公共字符串国家{get;set;}
公共字符串道路{get;set;}
公共字符串city{get;set;}
}
公共类功能
{
公共int id{get;set;}
公共质心质心{get;set;}
公共列表边界{get;set;}
公共属性属性{get;set;}
公共几何体{get;set;}
公共位置位置{get;set;}
公共字符串类型{get;set;}
}
公共类财产2
{
公共整数代码{get;set;}
公共列表坐标_顺序{get;set;}
}
公共类Crs
{
公共字符串类型{get;set;}
公共属性2属性{get;set;}
}
公共类根对象
{
找到公共int{get;set;}
公共列表边界{get;set;}
公共列表功能{get;set;}
公共字符串类型{get;set;}
公共Crs Crs{get;set;}
}
给你


有一个从json生成C类的工具。

我将如何解决这个问题

首先使用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;
}
公共类MainWrapper
{
找到公共int{get;set;}
公共列表边界{get;set;}
公共列表功能{get;set;}
公共Crs Crs{get;set;
}

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

谢谢Svarog,json对象是从web服务返回的,我无法简化它:(.你知道.net类会是什么样子吗?把你的JSON分解成最基本的元素,然后开始构建你的类。例如,你的类质心有两个字段:类型和坐标。一个类坐标只有两个浮点数。类特征有ID、质心、边界等等。最后y、 一个容器类,将有int-found,坐标[2]边界,一个功能列表,等等。从我这里得到+1-我以前从未见过这个工具-非常有用!Alex,很抱歉我错过了这个标记。我用非动态更新了我的答案version@LB你真棒,非常感谢。
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;
}