1.数据样式
{
"error_code": 0,
"reason": "SUCCESSED!",
"result": {
"totalCount": "1086",
"page": "1",
"num": "20",
"data": [
{
"symbol": "xx",
"name": "xx",
"trade": "xx",
"pricechange": "xx",
"changepercent": "xx",
"buy": "xx",
"sell": "xx",
"settlement": "xx",
"open": "",
"high": "",
"low": "",
"volume": 38781,
"amount": 45385925,
"code": "xx",
"ticktime": "xx"
}]
}
}
2.导入依赖
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
3.数据解析
@RequestMapping("/getJSON") @ResponseBody public String getJSON(Model model){ String url ="url"; //通过restTemplate 来获取json数据 ResponseEntity<String> results=restTemplate.exchange(url, HttpMethod.GET,null, String.class); String json=results.getBody(); //把数据进行解析 JSONObject jsonObject =JSONObject.fromObject(json); JSONObject result=jsonObject.getJSONObject("result"); JSONArray jsonArray= result.getJSONArray("data"); JSONObject data = null; for(int i=0;i<jsonArray.size();i++){ //遍历数组对象 data =jsonArray.getJSONObject(i); //将对象传给前端 model.addAttribute("json",data); System.out.println("id"+data.get("pricechange")+data.get("changepercent")+data.get("sell")); } //System.out.println(json); // model.addAllAttributes("json",json); return "index"; }
注意:
restTemplate的使用自己查,网上很多。