直接上代码:
public ActionResult FindMileage() { string s; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://yingyan.baidu.com/api/v3/track/getdistance?你的ak"); req.Method = "GET"; WebResponse wr = req.GetResponse(); Stream ResStream = wr.GetResponseStream(); Encoding encoding = Encoding.GetEncoding("UTF-8"); StreamReader streamReader = new StreamReader(ResStream, encoding); s=streamReader.ReadToEnd(); JSONObject obj = new JSONObject(s); string dis = obj.GetString("distance"); string test ="{"total":1,"rows":[{"VehicleID":"粤B8C97M","Mileage":“+dis+”}]}"; return Content(test); }
这里需要说明,百度地图返回的json是这样的:
{"status":0,"message":"成功","distance":82887.01672665}
我需要的是带total的,所以我用了JSONObject提取了一下东东,至于这个JSONObject怎么调用的,有一个大佬封装了一个dll,直接调用就ok了。你们想用的自己去GitHub上找吧