springboot 解析深层json

1、maven依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>

        <!-- JSONObject对象依赖的jar包 -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier><!-- 指定jdk版本 -->
        </dependency>
        <!-- Json依赖架包下载 -->

2、controller

@RestController
public class TestController {
    @Autowired
    ParseInfo parseInfo;
    @RequestMapping("/")
    public List<BalancerResponse> say()
    {

        RestTemplate restTemplate = new RestTemplate();
        String notice = restTemplate.getForObject("http://192.168.1.100:8888/json/lb_list", String.class);
        List<BalancerResponse> 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<BalancerResponse> 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<BalancerResponse> balancerResponseList = new ArrayList<>();
        JSONObject json = JSONObject.fromObject(notice);
        JSONArray jsonArray = json.getJSONArray("Destination");
        for(int i=0; i<jsonArray.size(); 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;

    }


}

 

posted @ 2021-02-16 16:01  jasonzhangxianrong  阅读(109)  评论(0编辑  收藏  举报