2013-07-08 129 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);