C#对象,用于从HERE Maps API反序列化JSON响应

时间:2019-02-03 20:42:14

标签: c# json here-maps-rest

我只是开始在这里使用REST API。 我有一些C#/ .NET编程经验,但是对REST API的访问不是很多。 我已经成功地成功调用了HERE Maps WebService并检索了JSON数据,但是我正在尝试找出从该数据中检索所需的特定信息的最佳方法。 我想我需要使用反序列化,但这似乎需要创建一个合适的C#对象进行反序列化。 我希望有一个可以在其中找到这些对象的存储库,而不必从头开始研究它们,但却找不到任何东西。 感谢收到任何建议。

谢谢

克里斯。

Json的回复:

IO

2 个答案:

答案 0 :(得分:1)

您可以选择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; }
    }
}

答案 1 :(得分:0)

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

克里斯。