针对Windows Phone的JSON反序列化

问题描述:

我想反序列化下面的JSON,但我真的不知道如何使用JSON.net来完成这项工作。我正在使用C#和JSON.Net库。针对Windows Phone的JSON反序列化

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

你不必声明许多小的类来反序列化。只需使用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"]); 
} 
+0

wp7不支持动态类型... – Alex 2012-03-13 09:52:58

+2

@亚历,对不起,我错过了那个标签。我用一个非动态版本更新了我的答案 – 2012-03-13 09:56:06

+0

@LB你很棒。非常感谢。 – Alex 2012-03-13 10:15:30

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

你有一个相当复杂的数据结构,所以为它创建一个类可能有点复杂。 您可能想要简化一点。

+0

谢谢Svarog,json对象是从Web服务返回的,我无法简化它:(。你有什么想法.net Class会是什么? – Alex 2012-03-13 08:53:57

+0

只要把你的JSON,分解成最基本的元素,并开始构建你的类。例如,您将拥有类Centroid,该类将具有两个字段:类型和坐标。一个类坐标将只是两个浮点数。类特征,这将具有ID,质心,界限等。最后,容器类将有int找到,坐标[2]边界,功能列表等 – Svarog 2012-03-13 09:10:31

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#类的工具。

+0

+1对我来说 - 因为我从来没有见过这个工具 - 知道的非常有用! – Stuart 2012-03-13 09:15:00

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

首先把你的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)