简体   繁体   中英

C# Objects to De-serialize JSON response from HERE Maps API

I'm just starting looking at REST API with HERE Maps. I've got some experience of C# / .NET programming, but not very much with accessing REST APIs. I have managed to successfully call the HERE Maps WebService and have retrieved the JSON data, but I'm trying to work out the best way to retrieve the specific information I need from this data. I think I need to use Deserialisation, but this seems to require the creation of a suitable C# object to de-serialse into. I was hoping that there is a repository somewhere where I could find these objects, rather than having to work them out from scratch, but can't find anything. Any suggestions gratefully received.

Thanks,

Chris.

Json response:

{
"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"
                       }
                    ]
                 }
              }
           }
        ]
     }
  ]
}
}

You can select JSON and paste C# classes in Visual Studio: Edit -> Paste Special -> Paste JSON as classes .

Then add NuGet package Newtonsoft.Json to solution and deserialize JSON to object, it will be looking as:

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

Many thanks to all the comments. Your suggestions have been filed away for future reference. In the end, because I only needed to access a couple of pieces of information, the whole converting to an object bitseemed a bit of overkill, so I got the result in XML instead, and used a couple of XML methods (GetElementsByTagName and ChildNodes) to extract the nodes I wanted. But thanks again for your suggestions, and I'm sure I will use at least some of them in the future :)

Chris.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM