2013-07-08 130 views
1

我有一个JSON结果这样访问元素使用C#

{ 
    "authenticationResultCode":"ValidCredentials", 
    "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png", 
    "copyright":"Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.", 
    "resourceSets":[ 
     { 
     "estimatedTotal":1, 
     "resources":[ 
      { 
       "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1", 
       "bbox":[ 
        47.636257744012461, 
        -122.13735364288299, 
        47.643983179153814, 
        -122.12206713944467 
       ], 
       "name":"1 Microsoft Way, Redmond, WA 98052", 
       "point":{ 
        "type":"Point", 
        "coordinates":[ 
        47.640120461583138, 
        -122.12971039116383 
        ] 
       }, 
       "address":{ 
        "addressLine":"1 Microsoft Way", 
        "adminDistrict":"WA", 
        "adminDistrict2":"King Co.", 
        "countryRegion":"United States", 
        "formattedAddress":"1 Microsoft Way, Redmond, WA 98052", 
        "locality":"Redmond", 
        "postalCode":"98052" 
       }, 
       "confidence":"High", 
       "entityType":"Address", 
       "geocodePoints":[ 
        { 
        "type":"Point", 
        "coordinates":[ 
         47.640120461583138, 
         -122.12971039116383 
        ], 
        "calculationMethod":"InterpolationOffset", 
        "usageTypes":[ 
         "Display" 
        ] 
        }, 
        { 
        "type":"Point", 
        "coordinates":[ 
         47.640144601464272, 
         -122.12976671755314 
        ], 
        "calculationMethod":"Interpolation", 
        "usageTypes":[ 
         "Route" 
        ] 
        } 
       ], 
       "matchCodes":[ 
        "Good" 
       ] 
      } 
     ] 
     } 
    ], 
    "statusCode":200, 
    "statusDescription":"OK", 
    "traceId":"b0b1286504404eafa7e7dad3e749d570" 
} 

我想要得到的对象的列表,每个对象将包含坐标

的价值那么,怎样才能访问这些元素的名字?

我使用C#作为背后的代码。

+0

你有没有尝试过使用一个包来反序列化JSON,比如Newtonsoft Json.NET? –

+0

我正在使用“System.Runtime.Serialization.Json.DataContractJsonSerializer” –

回答

0

对于此任务,您可以使用像Json.NET这样的软件包。

,轻松地就可以从http://json2csharp.com/

生成给予JSON字符串类,那么你可以访问项的属性如下下面

RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonText); 

来自json2csharp生成给出JSON

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

public class Address 
{ 
    public string addressLine { get; set; } 
    public string adminDistrict { get; set; } 
    public string adminDistrict2 { get; set; } 
    public string countryRegion { get; set; } 
    public string formattedAddress { get; set; } 
    public string locality { get; set; } 
    public string postalCode { get; set; } 
} 

public class GeocodePoint 
{ 
    public string type { get; set; } 
    public List<double> coordinates { get; set; } 
    public string calculationMethod { get; set; } 
    public List<string> usageTypes { get; set; } 
} 

public class Resource 
{ 
    public string __type { get; set; } 
    public List<double> bbox { get; set; } 
    public string name { get; set; } 
    public Point point { get; set; } 
    public Address address { get; set; } 
    public string confidence { get; set; } 
    public string entityType { get; set; } 
    public List<GeocodePoint> geocodePoints { get; set; } 
    public List<string> matchCodes { get; set; } 
} 

public class ResourceSet 
{ 
    public int estimatedTotal { get; set; } 
    public List<Resource> resources { get; set; } 
} 

public class RootObject 
{ 
    public string authenticationResultCode { get; set; } 
    public string brandLogoUri { get; set; } 
    public string copyright { get; set; } 
    public List<ResourceSet> resourceSets { get; set; } 
    public int statusCode { get; set; } 
    public string statusDescription { get; set; } 
    public string traceId { get; set; } 
} 
0

由于您已经使用DataContractJsonSerializer,所以我们坚持这一点。对json进行反序列化的最好方法是首先定义一个模型来捕获相关数据,例如

public class JsonModel 
{ 
    public int StatusCode { get; set; } 
    public string StatusDescription { get; set; } 
    public string TraceId { get; set; } 
    ... 
} 

接下来,装点模型所以它适合反序列化

[DataContract] 
public class JsonModel 
{ 
    [DataMember(Name = "statusCode")] 
    public int StatusCode { get; set; } 
    [DataMember(Name = "statusDescription")] 
    public string StatusDescription { get; set; } 
    [DataMember(Name = "traceId")] 
    public string TraceId { get; set; } 
    ... 
} 

于是最后,执行反序列化

using (var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonData))) 
{  
    var serializer = new DataContractJsonSerializer(typeof(JsonModel)); 
    var model = (JsonModel) serializer.ReadObject(memoryStream); 
    Console.WriteLine(model.StatusCode); 
} 

因此,通过名称如何访问这些元素?

反序列化的其他选项可以让您按名称引用属性,例如使用dynamic对象,例如,

var model = new JavaScriptSerializer().Deserialize<dynamic>(jsonData); 
Console.WriteLine(model["statusCode"]); 
0

所有Bing地图REST服务从URL中添加的类别下到项目中:

JSON Data Contracts

然后,请确保您添加使用指令:

using BingMapsRESTService.Common.JSON; 

并按如下所示读取字符串(其中stream是您的json的流):

var d = new DataContractJsonSerializer(typeof(Response)); 
var o = d.ReadObject(stream);