我是编程新手。我目前正在开发一个 map 功能,它需要我获取当前位置和最终位置之间的方向,但我不知道如何从 JSON RESPONSE 中提取文本。
此 JSON 响应是从 API 生成的。
这只是 JSON 响应的一部分。
{
"attributes" : {
"length" : 0.094387438,
"time" : 0.2831,
"text" : "Go west on _________",
"ETA" : 1365037200000,
"maneuverType" : "esriDMTStraight"
},
"compressedGeometry" : "+1+t1b+170r-2f-a-e-2"
}
我希望提取我显示的代码中的“文本”以将其显示在列表框中。
任何帮助将不胜感激。
最佳答案
您需要将 JSON 反序列化为 C# 类,您可以使用 Newtonsoft JSON.NET 转换器。要创建一个可以保存 JSON 对象的类,您可以复制示例 json 并将其粘贴到 http://json2csharp.com/ 中,这将为您提供 RootObject
类,从那里您可以访问 text
,这将在名为 text
的属性下可用。
对于上面的示例 JSON,您将获得如下类:
public class Attributes
{
public double length { get; set; }
public double time { get; set; }
public string text { get; set; }
public long ETA { get; set; }
public string maneuverType { get; set; }
}
public class RootObject
{
public Attributes attributes { get; set; }
public string compressedGeometry { get; set; }
}
关于c# - 提取部分 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15805175/