地理编码 REST 调用 C# 解析 JSON
Geocoding REST call C# Parsing JSON
我正在努力获取从下面的地理编码 REST 调用返回的数据(我已经删除了我的 app_id 和 app_code 所以你不能 运行 url 如下所示):
https://geocoder.api.here.com/6.2/geocode.json?app_id=[MY 应用 ID]&app_code=[我的应用代码]&searchtext=chester
可在此处找到示例响应:
https://developer.here.com/documentation/geocoder/topics/quick-start-geocode.html
我知道 returns 结果,但我似乎无法将结果传递到我的 C# 应用程序,我不知道是我构建的 class 系统不正确还是有什么问题否则我做错了。我已经构建了一个应用程序来读取位置 api 之前没有问题,但是这个似乎有点不同。
呼叫 returns 状态为 OK:
public void GeoCode()
{
// BUILD INITIAL STRING BASED ON PARAMETERS
string url = "https://geocoder.api.here.com/6.2/geocode.json?app_id=gpZ1Ym7rwuXs6Xj1TsD8&app_code=4UXmaGFTe30LttFgOY7iqQ&searchtext=" + tbLoc.text;
// RUN THE ASYNCRONOUS COMMAND (I THINK)
StartCoroutine(GetText(url));
}
IEnumerator GetText(string url)
{
// CREATE WEB REQUEST WITH SPECIFIED URL
UnityWebRequest www = UnityWebRequest.Get(url);
// RETURN RESULTS FROM ASYNC COMMAND
yield return www.SendWebRequest();
// PARSE JSON RESPONSE
RootObject RootObject = JsonUtility.FromJson<RootObject>(www.downloadHandler.text);
// IF 200 (STATUS OKAY) GATHER RESULTS, ELSE RETURN ERROR
if (www.responseCode == 200)
{
BuildArray(RootObject);
}
else
{
Debug.Log("Call Failed With Error: " + www.responseCode.ToString());
}
}
private void BuildArray(RootObject RootObject)
{
print(RootObject.Response.View[0].Result[0].Location.Address.Label);
}
它尝试 运行 BuildArray 函数,但此 returns 为空引用。 (我只是使用 NextPageInformation 作为最基本的选项来测试)
下面是我为处理 JSON:
而构建的 class 结构
public class Address
{
public string Label { get; set; }
}
public class Location
{
public Address Address { get; set; }
}
public class Result
{
public Location Location { get; set; }
}
public class View
{
public List<Result> Result { get; set; }
}
public class Response
{
public List<View> View { get; set; }
}
public class RootObject
{
public Response Response { get; set; }
}
有人可以帮忙吗?
谢谢
我个人在使用 JsonUtility 解析嵌套的 json 对象时遇到了问题。所以我切换到 Newtonsoft Json 并且效果非常好。
在您的情况下,您需要使用 [Serializable] see here
将 类 标记为序列化
[Serializable]
public class Address
{
public string Label;
}
如果您只想从 json 中解析一个键值对,那么创建 class 结构对您来说将是一项乏味的工作。
您可以 Querying a json with Newtonsoft.
而不是创建 class 结构
- 解析您的 json。
- 查询您已解析的 json 以检索
Label
值。
//1
JToken jToken = JToken.Parse(jsonString);
//2
string label = jToken["Response"]["View"][0]["Result"][0]["Location"]["Address"]["Label"].ToObject<string>();
Console.WriteLine(label);
Console.ReadLine();
输出:(来自 link 中提供的样本 json)
注意:您需要从 nuget 安装 newtonsoft.json 包并将以下命名空间添加到您的程序中。
using Newtonsoft.Json.Linq;
编辑:
如果你想解析你的完整 json 然后首先创建一个 class 结构如下
public class MetaInfo
{
public DateTime Timestamp { get; set; }
}
public class MatchQuality
{
public int City { get; set; }
public List<double> Street { get; set; }
public int HouseNumber { get; set; }
}
public class DisplayPosition
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class NavigationPosition
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class TopLeft
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class BottomRight
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class MapView
{
public TopLeft TopLeft { get; set; }
public BottomRight BottomRight { get; set; }
}
public class AdditionalData
{
public string value { get; set; }
public string key { 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 District { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
public string PostalCode { get; set; }
public List<AdditionalData> AdditionalData { get; set; }
}
public class Location
{
public string LocationId { get; set; }
public string LocationType { get; set; }
public DisplayPosition DisplayPosition { get; set; }
public List<NavigationPosition> NavigationPosition { get; set; }
public MapView MapView { get; set; }
public Address Address { get; set; }
}
public class Result
{
public int Relevance { get; set; }
public string MatchLevel { get; set; }
public MatchQuality MatchQuality { get; set; }
public string MatchType { get; set; }
public Location Location { get; set; }
}
public class View
{
public string _type { get; set; }
public int ViewId { get; set; }
public List<Result> Result { get; set; }
}
public class Response
{
public MetaInfo MetaInfo { get; set; }
public List<View> View { get; set; }
}
public class RootObject
{
public Response Response { get; set; }
}
然后你可以反序列化你的 json 喜欢。
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
注意:您需要将以下命名空间添加到您的程序中。
using Newtonsoft.Json;
我正在努力获取从下面的地理编码 REST 调用返回的数据(我已经删除了我的 app_id 和 app_code 所以你不能 运行 url 如下所示):
https://geocoder.api.here.com/6.2/geocode.json?app_id=[MY 应用 ID]&app_code=[我的应用代码]&searchtext=chester
可在此处找到示例响应:
https://developer.here.com/documentation/geocoder/topics/quick-start-geocode.html
我知道 returns 结果,但我似乎无法将结果传递到我的 C# 应用程序,我不知道是我构建的 class 系统不正确还是有什么问题否则我做错了。我已经构建了一个应用程序来读取位置 api 之前没有问题,但是这个似乎有点不同。
呼叫 returns 状态为 OK:
public void GeoCode()
{
// BUILD INITIAL STRING BASED ON PARAMETERS
string url = "https://geocoder.api.here.com/6.2/geocode.json?app_id=gpZ1Ym7rwuXs6Xj1TsD8&app_code=4UXmaGFTe30LttFgOY7iqQ&searchtext=" + tbLoc.text;
// RUN THE ASYNCRONOUS COMMAND (I THINK)
StartCoroutine(GetText(url));
}
IEnumerator GetText(string url)
{
// CREATE WEB REQUEST WITH SPECIFIED URL
UnityWebRequest www = UnityWebRequest.Get(url);
// RETURN RESULTS FROM ASYNC COMMAND
yield return www.SendWebRequest();
// PARSE JSON RESPONSE
RootObject RootObject = JsonUtility.FromJson<RootObject>(www.downloadHandler.text);
// IF 200 (STATUS OKAY) GATHER RESULTS, ELSE RETURN ERROR
if (www.responseCode == 200)
{
BuildArray(RootObject);
}
else
{
Debug.Log("Call Failed With Error: " + www.responseCode.ToString());
}
}
private void BuildArray(RootObject RootObject)
{
print(RootObject.Response.View[0].Result[0].Location.Address.Label);
}
它尝试 运行 BuildArray 函数,但此 returns 为空引用。 (我只是使用 NextPageInformation 作为最基本的选项来测试)
下面是我为处理 JSON:
而构建的 class 结构 public class Address
{
public string Label { get; set; }
}
public class Location
{
public Address Address { get; set; }
}
public class Result
{
public Location Location { get; set; }
}
public class View
{
public List<Result> Result { get; set; }
}
public class Response
{
public List<View> View { get; set; }
}
public class RootObject
{
public Response Response { get; set; }
}
有人可以帮忙吗? 谢谢
我个人在使用 JsonUtility 解析嵌套的 json 对象时遇到了问题。所以我切换到 Newtonsoft Json 并且效果非常好。 在您的情况下,您需要使用 [Serializable] see here
将 类 标记为序列化[Serializable]
public class Address
{
public string Label;
}
如果您只想从 json 中解析一个键值对,那么创建 class 结构对您来说将是一项乏味的工作。
您可以 Querying a json with Newtonsoft.
而不是创建 class 结构- 解析您的 json。
- 查询您已解析的 json 以检索
Label
值。
//1
JToken jToken = JToken.Parse(jsonString);
//2
string label = jToken["Response"]["View"][0]["Result"][0]["Location"]["Address"]["Label"].ToObject<string>();
Console.WriteLine(label);
Console.ReadLine();
输出:(来自 link 中提供的样本 json)
注意:您需要从 nuget 安装 newtonsoft.json 包并将以下命名空间添加到您的程序中。
using Newtonsoft.Json.Linq;
编辑:
如果你想解析你的完整 json 然后首先创建一个 class 结构如下
public class MetaInfo
{
public DateTime Timestamp { get; set; }
}
public class MatchQuality
{
public int City { get; set; }
public List<double> Street { get; set; }
public int HouseNumber { get; set; }
}
public class DisplayPosition
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class NavigationPosition
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class TopLeft
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class BottomRight
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class MapView
{
public TopLeft TopLeft { get; set; }
public BottomRight BottomRight { get; set; }
}
public class AdditionalData
{
public string value { get; set; }
public string key { 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 District { get; set; }
public string Street { get; set; }
public string HouseNumber { get; set; }
public string PostalCode { get; set; }
public List<AdditionalData> AdditionalData { get; set; }
}
public class Location
{
public string LocationId { get; set; }
public string LocationType { get; set; }
public DisplayPosition DisplayPosition { get; set; }
public List<NavigationPosition> NavigationPosition { get; set; }
public MapView MapView { get; set; }
public Address Address { get; set; }
}
public class Result
{
public int Relevance { get; set; }
public string MatchLevel { get; set; }
public MatchQuality MatchQuality { get; set; }
public string MatchType { get; set; }
public Location Location { get; set; }
}
public class View
{
public string _type { get; set; }
public int ViewId { get; set; }
public List<Result> Result { get; set; }
}
public class Response
{
public MetaInfo MetaInfo { get; set; }
public List<View> View { get; set; }
}
public class RootObject
{
public Response Response { get; set; }
}
然后你可以反序列化你的 json 喜欢。
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
注意:您需要将以下命名空间添加到您的程序中。
using Newtonsoft.Json;