c# - 用于反序列化来自 HERE Maps API 的 JSON 响应的 C# 对象
问题描述
我刚刚开始使用 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"
}
]
}
}
}
]
}
]
}
}
解决方案
您可以选择 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; }
}
}
推荐阅读
- macos - OS X 10.15 (Catalina) 上的 GDB 在第一次执行时卡住
- r - 当 x 具有几何分布时,如何使用 sample() 函数从 x 生成 n 个随机变量?
- xcode - 如何更改 Xcode 项目,这样我就不必在 SUDO 模式下运行它
- sql-server - 出现“无法打开访客会话”错误
- xamarin - 属性“内容”设置不止一次 - XAML
- python - KeyError:使用带有布尔值的 get_loc 时为真
- java - 如何使用 Elasticsearch API ScriptQueryBuilder
- angular - 如何在我的 Angular 应用程序中调试 node_module?
- java - 在类的类型参数上设置实际类型的所有方法是什么
- html - CSS渐变重复