从这里Maps API的C#对象反序列化JSON响应

问题描述 投票:0回答:2

我刚开始在这里的地图看REST API。我已经有了一个C#/。NET编程经验,但不是很多与访问REST的API。我已经设法成功地调用HERE地图的WebService,并已获取的JSON数据,但我试图找出取回我从这个数据所需的特定信息的最佳途径。我想我需要使用Deserialisation,但这似乎需要建立一个适当的C#对象的脱serialse成。我希望有一个仓库的地方在那里我能找到这些对象,而不是将它们从无到有工作了,但不能发现任何东西。任何建议感激地接受。

谢谢,

克里斯。

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"
                       }
                    ]
                 }
              }
           }
        ]
     }
  ]
}
}
c# json here-maps-rest
2个回答
1
投票

您可以选择JSON和粘贴Visual Studio: Edit -> Paste Special -> Paste JSON as classes C#类。

然后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; }
    }
}

0
投票

非常感谢所有的评论。您的建议已经提交,以备将来参考。最后,因为我只需要访问一对夫妇的信息,整个转换为对象bitseemed有点大材小用,所以我得到的结果以XML代替,并用一对夫妇的XML方法(的getElementsByTagName和的childNodes)提取我想要的节点。但是,再次感谢您的建议,我敢肯定,我会使用至少其中一些在未来:)

克里斯。

© www.soinside.com 2019 - 2024. All rights reserved.