RestTemplate get和post简单用法:
参考文章:https://my.oschina.net/u/2984715/blog/1805550
近期同事项目中需要调用接口接收json格式的嵌套数据,并解析出来,发现了一个spring自带的一个工具类,用于发送参数请求并接收结果,参考上面的文章简单分享一下:
json格式数据如下:
{
"analytics": {
"VPTUGuanL": {
"configuration": {
"33018Ultimate": 10,
"33020Deluxe": 22,
"33020Comfortable": 3,
"33018Deluxe": 5,
"38020Deluxe": 5,
"33020Custom": 18,
"33018Comfortable": 3,
"33020Ultimate ": 15,
"38020Ultimate": 5,
"33018Custom": 5,
"38020Exalted": 4
},
"appearance": {
"BAB": 42,
"DG": 41,
"AW": 33,
"IS": 40,
"SO": 31,
"MPB": 39
},
"interior": {
"EPB": 47,
"BAW": 29,
"BAWO": 34
},
"package": {
"19Hub": 10,
"Electric": 21,
"18Double": 10
},
"Complete": {
"Complete": 61
}
},
"VPHuiAng": {
"configuration": {
"380DE": 40,
"380DU": 10,
"480Ul": 16,
"380BE": 29,
"380Ul": 28,
"480DE": 16,
"480DU": 17
},
"appearance": {
"PB": 41,
"OA": 27,
"PS": 26,
"PW": 40,
"MRR": 23,
"GS": 35,
"VB": 39,
"SWB": 38
},
"interior": {
"RR": 35,
"MB": 53,
"EB": 40,
"TB": 31
},
"package": {
"HeadUp": 89,
"FullLED": 9,
"18Polished": 48,
"Driver": 63,
"19Aluminum": 45,
"BrightLED": 11,
"HiTech": 161,
"18Aluminum": 12
},
"Complete": {
"Complete": 128
}
},
"VPGTS": {
"configuration": {
"GTS": 4
},
"appearance": {
"BW": 27,
"HR": 16,
"WB": 19
},
"interior": {
"AB": 7
},
"package": {
"Blind": 11,
"Multiple": 9,
"Enjoy": 12,
"Listen": 9,
"FullTime": 7
},
"Complete": {
"CompleteConfiguration": 51
}
},
"VPTuAng": {
"configuration": {
"530EU": 5,
"330Ul": 12,
"530DE": 10,
"330CE": 7,
"380DE": 8,
"380CE": 5,
"330DE": 9,
"380Ul": 40,
"530Ul": 7
},
"appearance": {
"AR": 30,
"OA": 27,
"DG": 26,
"BAB": 28,
"AW": 32,
"IS": 31,
"VB": 25,
"MPB": 30
},
"interior": {
"NB": 54,
"TB": 61,
"RFR": 62
},
"package": {
"PLA": 36,
"6S": 17
},
"Complete": {
"Complete": 73
}
},
"ExperienceEffect": {
"VehiclePreference": {
"VPTuGuanL": 277,
"VPHuiAng": 677,
"VPGTS": 165,
"VPTuAng": 259
},
"Transformation": {
"FourthStep": 362,
"FirstStep": 1378,
"FifthStep": 313,
"SecondStep": 805,
"SixthStep": 109,
"ThirdStep": 496
}
}
}
}
如果我们按照常规发送post请求接收json数据,代码有点多,用上述工具可以轻松接收!
public void testData()
{
//接口地址
String url = "http://XXXXXXXX";
// String url = "http://IP:8080/demo/demoTest2";
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
// headers.set("phone", "123456");
Map<String, Object> params = new HashMap<>();
//提交的参数
params.put("start_time", "20180824");
params.put("end_time", "20180827");
//所需工具类
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(params, null);
//postForEntity:在发送post请求时,使用该方法
ResponseEntity<String> request = restTemplate.postForEntity(url, httpEntity, String.class);
String firstJsonStr = request.getBody().toString();
这样就能根据我们发送的时间请求接收到json格式的数据了,下一步,解析:我使用的是jsonObject方法循环,如有更简单的,请各位博友不吝赐教!!!
JSONObject jsonObject = JSONObject.fromObject(firstJsonStr);
//去除analytics层
String secondJsonStr = jsonObject.optString("analytics");
jsonObject = JSONObject.fromObject(secondJsonStr);
for (Iterator<String> iterator = jsonObject.keys();iterator.hasNext();)
{
String key = iterator.next();
//配置参数和最后一项数据
String configuration_VehiclePreference = jsonObject.get(key).toString();
JSONObject secondObject = JSONObject.fromObject(configuration_VehiclePreference);
for(Iterator<String> secondIterator = secondObject.keys();secondIterator.hasNext();)
{
//最后一层json的key
String lastJsonKey = secondIterator.next();
//最后一层json数据
String lastJsonStr = secondObject.get(lastJsonKey).toString();
JSONObject thirdObject = JSONObject.fromObject(lastJsonStr);
for(Iterator<String> thirdIterator = thirdObject.keys();thirdIterator.hasNext();)
{
//低层数据的key
String detailKey = thirdIterator.next();
//低层详细数据
String str = thirdObject.get(detailKey).toString();
//打印最底层的数据,如低层的数字
System.out.println(str);
}
}
}