【fastjson】获取key

使用fastjson获取JSONObject中未知的key和对应的value值

1.maven依赖


    com.alibaba
    fastjson
    1.2.72

2.实现代码

public static void main(String[] args) {
        String a = "[{\"code\":\"OY03_1\",\"month\":0,\"tptype\":\"OY03\",\"year\":2021,\"subjects\":[{\"cells\":[{\"code\":\"3\",\"name\":\"年初数\",\"value\":\"0.00\"},{\"code\":\"4\",\"name\":\"年末数\",\"value\":\"0.00\"}],\"code\":\"E1\",\"name\":\"短期借款\",\"id\":\"0038\"},{\"cells\":[{\"code\":\"3\",\"name\":\"年初数\",\"value\":\"417,916.37\"},{\"code\":\"4\",\"name\":\"年末数\",\"value\":\"414,091.76\"}],\"code\":\"A1\",\"name\":\"货币资金\",\"id\":\"0001\"},{\"cells\":[{\"code\":\"3\",\"name\":\"年初数\",\"value\":\"0.00\"},{\"code\":\"4\",\"name\":\"年末数\",\"value\":\"0.00\"}],\"code\":\"A2\",\"name\":\"交易性金融资产\",\"id\":\"0002\"},{\"cells\":[{\"code\":\"3\",\"name\":\"年初数\",\"value\":\"0.00\"},{\"code\":\"4\",\"name\":\"年末数\",\"value\":\"0.00\"}],\"code\":\"E2\",\"name\":\"交易性金融负债\",\"id\":\"0039\"},{\"cells\":[{\"code\":\"3\",\"name\":\"年初数\",\"value\":\"0.00\"},{\"code\":\"4\",\"name\":\"年末数\",\"value\":\"0.00\"}],\"code\":\"E3\",\"name\":\"应付票据\",\"id\":\"0040\"},{\"cells\":[{\"code\":\"3\",\"name\":\"年初数\",\"value\":\"0.00\"},{\"code\":\"4\",\"name\":\"年末数\",\"value\":\"0.00\"}],\"code\":\"A3\",\"name\":\"应收票据\",\"id\":\"0003\"},{\"cells\":[{\"code\":\"3\",\"name\":\"年初数\",\"value\":\"0.00\"},{\"code\":\"4\",\"name\":\"年末数\",\"value\":\"0.00\"}],\"code\":\"E4\",\"name\":\"应付账款\",\"id\":\"00";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("09014431",a);
        log.info("jsonObject:{}",jsonObject);

        List keyList = new ArrayList<>();

        Set> entries = jsonObject.entrySet();
        for (Map.Entry entry : entries) {
            String key = entry.getKey();
            log.info("key:{}",key);
            keyList.add(key);
            Object value = entry.getValue();
            log.info("value:{}",value);
        }
        log.info("--------------------------------------------");
        for (String key : keyList) {
            Object value = jsonObject.get(key);
            log.info("value:{}",value);
        }
    }

【fastjson】获取key_第1张图片

你可能感兴趣的:(Java,java,fastjson)