码迷,mamicode.com
首页 > 编程语言 > 详细

springboot 解析深层json

时间:2021-02-18 13:29:38      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:net   hash   split   response   sources   group   code   @Value   version   

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;

    }


}

 

springboot 解析深层json

标签:net   hash   split   response   sources   group   code   @Value   version   

原文地址:https://www.cnblogs.com/zhangxianrong/p/14406803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!