springboot 解析深层json
标签:net hash split response sources group code @Value version
1、maven依赖
com.alibaba
fastjson
1.2.28commons-beanutils
commons-beanutils
1.9.3commons-collections
commons-collections
3.2.1commons-lang
commons-lang
2.6commons-logging
commons-logging
1.1.1net.sf.ezmorph
ezmorph
1.0.6net.sf.json-lib
json-lib
2.2.3jdk15
2、controller
@RestController
public class TestController {
@Autowired
ParseInfo parseInfo;
@RequestMapping("/")
public List say()
{
RestTemplate restTemplate = new RestTemplate();
String notice = restTemplate.getForObject("http://192.168.1.100:8888/json/lb_list", String.class);
List result = parseInfo.parserToMap(notice);
return result;
}
}
3、service
@Service
public class ParseInfo {
@Value("${configtxt.path}")
private String configtxtPath;
private static Map map = new HashMap();
private static Map ipPortMap = new HashMap();
public List parserToMap(String notice)
{
try{
File file = new File(configtxtPath);
InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));
BufferedReader bf = new BufferedReader(inputReader);
// 按行读取字符串
String str;
while ((str = bf.readLine()) != null) {
ipPortMap.put(str.split(",")[0], str.split(",")[1]);
}
bf.close();
inputReader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
List balancerResponseList = new ArrayList();
JSONObject json = JSONObject.fromObject(notice);
JSONArray jsonArray = json.getJSONArray("Destination");
for(int i=0; i)
{
BalancerResponse balancerResponse = new BalancerResponse();
String ip = jsonArray.getJSONObject(i).getString("value").split(":")[1];
String maxLoad = jsonArray.getJSONObject(i).getJSONObject("children").getJSONObject("Resources")
.getJSONObject("children").getJSONArray("Resource").getJSONObject(0).getJSONObject("attributes")
.getString("max");
String currentLoad = jsonArray.getJSONObject(i).getJSONObject("children").getJSONObject("Resources")
.getJSONObject("children").getJSONArray("Resource").getJSONObject(0).getJSONObject("attributes")
.getString("load");
;
balancerResponse.setHost(ip);
balancerResponse.setPort(ipPortMap.get(ip).toString());
balancerResponse.setMaxLoad(Integer.parseInt(maxLoad));
balancerResponse.setCurrentLoad(Integer.parseInt(currentLoad));
balancerResponse.setPass("ClueCon");
balancerResponseList.add(balancerResponse);
}
System.out.println(ipPortMap);
return balancerResponseList;
}
}
springboot 解析深层json
标签:net hash split response sources group code @Value version
原文地址:https://www.cnblogs.com/zhangxianrong/p/14406803.html
评论