0

我刚刚开始使用 HERE 地图查看 REST API。我有一些 C# / .NET 编程经验,但在访问 REST API 方面并不多。我已成功调用 HERE Maps WebService 并检索了 JSON 数据,但我正在尝试找出从这些数据中检索我需要的特定信息的最佳方法。我想我需要使用反序列化,但这似乎需要创建一个合适的 C# 对象来反序列化。我希望在某个地方有一个存储库,我可以在其中找到这些对象,而不必从头开始处理它们,但找不到任何东西。任何建议都非常感激。

谢谢,

克里斯。

json响应:

{
"Response":{
  "MetaInfo":{
     "Timestamp":"2019-02-03T20:41:00.395+0000"
  },
  "View":[
     {
        "_type":"SearchResultsViewType",
        "ViewId":0,
        "Result":[
           {
              "Relevance":1.0,
              "MatchLevel":"postalCode",
              "MatchQuality":{
                 "PostalCode":1.0
              },
              "Location":{
                 "LocationId":"NT_CwZliV687TLYW4ZZKm4VNA",
                 "LocationType":"point",
                 "DisplayPosition":{
                    "Latitude":50.8082,
                    "Longitude":-0.39127
                 },
                 "NavigationPosition":[
                    {
                       "Latitude":50.8082,
                       "Longitude":-0.39127
                    }
                 ],
                 "MapView":{
                    "TopLeft":{
                       "Latitude":50.82169,
                       "Longitude":-0.41262
                    },
                    "BottomRight":{
                       "Latitude":50.79471,
                       "Longitude":-0.36992
                    }
                 },
                 "Address":{
                    "Label":"BN11 3PQ, Worthing, England, United Kingdom",
                    "Country":"GBR",
                    "State":"England",
                    "County":"West Sussex",
                    "City":"Worthing",
                    "PostalCode":"BN11 3PQ",
                    "AdditionalData":[
                       {
                          "value":"United Kingdom",
                          "key":"CountryName"
                       },
                       {
                          "value":"England",
                          "key":"StateName"
                       },
                       {
                          "value":"West Sussex",
                          "key":"CountyName"
                       }
                    ]
                 }
              }
           }
        ]
     }
  ]
}
}
4

2 回答 2

3

您可以选择 JSON 并将 C# 类粘贴到Visual Studio: Edit -> Paste Special -> Paste JSON as classes.

然后将 NuGet 包Newtonsoft.Json添加到解决方案并将 JSON 反序列化为对象,它将如下所示:

using Newtonsoft.Json;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = "";//download json

            Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(json);

            DateTime tt = obj.Response.MetaInfo.Timestamp;
        }
    }    

    public class Rootobject
    {
        public Response Response { get; set; }
    }

    public class Response
    {
        public Metainfo MetaInfo { get; set; }
        public View[] View { get; set; }
    }

    public class Metainfo
    {
        public DateTime Timestamp { get; set; }
    }

    public class View
    {
        public string _type { get; set; }
        public int ViewId { get; set; }
        public Result[] Result { get; set; }
    }

    public class Result
    {
        public float Relevance { get; set; }
        public string MatchLevel { get; set; }
        public Matchquality MatchQuality { get; set; }
        public Location Location { get; set; }
    }

    public class Matchquality
    {
        public float PostalCode { get; set; }
    }

    public class Location
    {
        public string LocationId { get; set; }
        public string LocationType { get; set; }
        public Displayposition DisplayPosition { get; set; }
        public Navigationposition[] NavigationPosition { get; set; }
        public Mapview MapView { get; set; }
        public Address Address { get; set; }
    }

    public class Displayposition
    {
        public float Latitude { get; set; }
        public float Longitude { get; set; }
    }

    public class Mapview
    {
        public Topleft TopLeft { get; set; }
        public Bottomright BottomRight { get; set; }
    }

    public class Topleft
    {
        public float Latitude { get; set; }
        public float Longitude { get; set; }
    }

    public class Bottomright
    {
        public float Latitude { get; set; }
        public float Longitude { get; set; }
    }

    public class Address
    {
        public string Label { get; set; }
        public string Country { get; set; }
        public string State { get; set; }
        public string County { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
        public Additionaldata[] AdditionalData { get; set; }
    }

    public class Additionaldata
    {
        public string value { get; set; }
        public string key { get; set; }
    }

    public class Navigationposition
    {
        public float Latitude { get; set; }
        public float Longitude { get; set; }
    }
}
于 2019-02-03T20:57:48.133 回答
0

非常感谢所有的评论。您的建议已归档以备将来参考。最后,因为我只需要访问几条信息,整个转换为一个对象有点矫枉过正,所以我得到了 XML 的结果,并使用了几个 XML 方法(GetElementsByTagName 和 ChildNodes)提取我想要的节点。但是再次感谢您的建议,我相信我将来至少会使用其中一些:)

克里斯。

于 2019-02-05T21:01:34.420 回答