1

我有这样的 JSON 结果

{
   "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# 作为后面的代码。

4

3 回答 3

0

您可以使用Json.NET 之类的包来完成此任务。

您可以轻松地通过从http://json2csharp.com/提供 json 字符串来生成类

然后您可以访问项目的属性,如下所示

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

下面是从给定 json 的 json2csharp 生成的类

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; }
}
于 2013-07-08T09:48:02.510 回答
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"]);
于 2013-07-08T09:57:26.423 回答
0

将以下 URL 中的所有 Bing 地图 REST 服务的类添加到您的项目中:

JSON 数据合约

然后,确保添加 using 指令:

using BingMapsRESTService.Common.JSON;

并按如下方式读取字符串(stream您的 json 的流在哪里):

var d = new DataContractJsonSerializer(typeof(Response));
var o = d.ReadObject(stream);
于 2013-07-08T10:02:43.360 回答