1、远程调用第三方项目,获取json数据之后进行解析。
第三方数据:
2、直接上代码:
package com.hera.test;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.huntersun.tool.HttpRequestService;
import com.huntersun.tool.exception.HttpRequestException;
public class TestAccess {
public static void main(String[] args) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("longitude", "106.64");
dataMap.put("latitude", "26.59");
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer();
String data = null;
try {
// 获取数据信息 远程调用 httpclient
data = HttpRequestService.httpPost("http://testapi.zhixingtcc.com/geo/carGeo/queryNearbyCar",dataMap);
if (data != null) {
JSONObject json = new JSONObject(data);
JSONObject jsonObj = json.getJSONObject("data");//获取json数组中的data项
JSONArray paths = jsonObj.getJSONArray("carItem");
for (int i = 0; i < paths.length(); i++) {
String carNo = paths.getJSONObject(i).getString("carNo");
String carColor = paths.getJSONObject(i).getString("carColor");
Double distance = paths.getJSONObject(i).getDouble("distance");
Double longitude = paths.getJSONObject(i).getDouble("longitude");
Double latitude = paths.getJSONObject(i).getDouble("latitude");
sb.append("\"" + carNo + "\"" + ",");
System.out.println(carNo+"--"+carColor +"--" + distance +"-" +longitude + "--" + latitude);
}
}
} catch (HttpRequestException e) {
e.printStackTrace();
}
System.out.println(sb1.length());
String carNos = sb.toString();
carNos = carNos.substring(0,carNos.length() - 1);
System.out.println(carNos);
}
}
|
3:结果: