json序列化获取高德Api实现路径查询
作者:互联网
前端:
<div> 起点:<input id="adress" type="text" runat="server" /> <br /> 终点:<input id="Address" type="text" runat="server" /> <br /> 天气:<input id="weather" type="text" runat="server" /> <br /> <asp:RadioButtonList ID="radlRoule" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem value="0" >步行</asp:ListItem> <asp:ListItem Value="1">驾车</asp:ListItem> <asp:ListItem Value="2">公交</asp:ListItem> </asp:RadioButtonList> <br /> <asp:Button ID="Button1" runat="server" Text="开始导航" OnClick="Button1_Click" /><br /> </div>
后台:
public JObject GetObj(string url) { WebClient client = new WebClient(); byte[] byteArray = client.DownloadData(url); string html = Encoding.UTF8.GetString(byteArray); return (JObject)JsonConvert.DeserializeObject(html); }
protected void Button1_Click(object sender, EventArgs e)
{
WebClient client = new WebClient();
//天气
string WeatherUrl = "http://restapi.amap.com/v3/weather/weatherInfo?key=自己的&city="+weather.Value+"";
JObject objweather = GetObj(WeatherUrl);
Response.Write(objweather["lives"][0]["city"].ToString()+":" + objweather["lives"][0]["weather"].ToString()+"," + objweather["lives"][0]["temperature"].ToString()+"摄氏度" + "<br/>");
//起点经纬度
string StartPoint= GetLatitude(adress.Value);
//终点经纬度
string EndPoint= GetLatitude(Address.Value);
//请求连接https://restapi.amap.com/v3/direction/driving?origin=116.45925,39.910031&destination=116.587922,40.081577&output=json&key=自己的
string LinkUrl = "";
if (radlRoule.Text=="0")
{
//步行
LinkUrl = "https://restapi.amap.com/v3/direction/walking?origin=" + StartPoint + "&destination=" + EndPoint + "&output=json&key=自己的";
byte[] byteArray = client.DownloadData(LinkUrl);
string html = Encoding.UTF8.GetString(byteArray);
JObject obj = (JObject)JsonConvert.DeserializeObject(html);
string m = obj["route"]["paths"][0]["steps"].ToString();
JArray array = (JArray)JsonConvert.DeserializeObject(m);
for (int i = 0; i < array.Count; i++)
{
Response.Write(obj["route"]["paths"][0]["steps"][i]["instruction"].ToString() + "<br/>");
}
}
else if(radlRoule.Text == "1")//驾车
{
LinkUrl = "https://restapi.amap.com/v3/direction/driving?origin=" + StartPoint + "&destination=" + EndPoint + "&output=json&key=自己的";
byte[] byteArray = client.DownloadData(LinkUrl);
string html = Encoding.UTF8.GetString(byteArray);
JObject obj = (JObject)JsonConvert.DeserializeObject(html);
string m = obj["route"]["paths"][0]["steps"].ToString();
JArray array = (JArray)JsonConvert.DeserializeObject(m);
for (int i = 0; i < array.Count; i++)
{
Response.Write(obj["route"]["paths"][0]["steps"][i]["instruction"].ToString() + "<br/>");
}
}
else//公交
{
LinkUrl = "https://restapi.amap.com/v3/direction/transit/integrated?origin=" + StartPoint + "&destination=" + EndPoint + "&city=010&output=json&key=自己的";
byte[] byteArray = client.DownloadData(LinkUrl);
string html = Encoding.UTF8.GetString(byteArray);
JObject obj = (JObject)JsonConvert.DeserializeObject(html);
string m = obj["route"]["transits"][0]["segments"][0]["bus"]["buslines"][0]["via_stops"].ToString();//每站的路线
string n = obj["route"]["transits"][0]["segments"][0]["bus"]["buslines"].ToString();
JArray array = (JArray)JsonConvert.DeserializeObject(m);
JArray arrayn = (JArray)JsonConvert.DeserializeObject(n);
for (int j=0;j< arrayn.Count; j++)
{
Response.Write(obj["route"]["transits"][0]["segments"][0]["bus"]["buslines"][j]["name"]);
for (int i = 0; i < array.Count; i++)
{
Response.Write(obj["route"]["transits"][0]["segments"][0]["bus"]["buslines"][0]["via_stops"][i]["name"].ToString() + "<br/>");
}
}
}
//Response.Write(StartPoint);
//Response.Write(EndPoint);
}
public string GetLatitude(string Address)
{
WebClient client = new WebClient();
string address = Address;// "洛阳河南科技大学";
string dts = "https://restapi.amap.com/v3/geocode/geo?address=" + address + "&key=自己的";
byte[] byteArray = client.DownloadData(dts);
string html = Encoding.UTF8.GetString(byteArray);
//反序列化
JObject obj = (JObject)JsonConvert.DeserializeObject(html);
return obj["geocodes"][0]["location"].ToString();
}
标签:byteArray,obj,string,JObject,html,json,Api,ToString,序列化 来源: https://www.cnblogs.com/shuicai/p/14943303.html