[英]C# Objects to De-serialize JSON response from HERE Maps API
I'm just starting looking at REST API with HERE Maps. 我只是开始在这里查看带有RERE Maps的REST API。 I've got some experience of C# / .NET programming, but not very much with accessing REST APIs. 我有一些C#/ .NET编程经验,但是对REST API的访问不是很多。 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. 我已经成功地成功调用了HERE Maps WebService并检索了JSON数据,但是我正在尝试找出从该数据中检索所需的特定信息的最佳方法。 I think I need to use Deserialisation, but this seems to require the creation of a suitable C# object to de-serialse into. 我想我需要使用反序列化,但这似乎需要创建一个合适的C#对象进行反序列化。 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
. 您可以选择JSON并在Visual Studio: Edit -> Paste Special -> Paste JSON as classes
粘贴C#类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: 然后将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; }
}
}
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. 最后,由于我只需要访问几条信息,整个转换成一个对象似乎有点过大了,所以我改为使用XML来获得结果,并使用了几种XML方法(GetElementsByTagName和ChildNodes)提取我想要的节点。 But thanks again for your suggestions, and I'm sure I will use at least some of them in the future :) 但是再次感谢您的建议,我相信以后我会至少使用其中的一些:)
Chris. 克里斯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.