2020-09-30

使用Jackson递归获取json数据的树形结构

getNext获取当前jsonNode节点的每个key,然后判断每个value是否是一个节点,接着再判断是否是一个数组

public static void getNext(JsonNode jsonNode,String str){
        for(Iterator<Map.Entry<String,JsonNode>> it = jsonNode.fields(); it.hasNext();){
            Map.Entry<String,JsonNode> m = it.next();
            String s = m.getKey();
            JsonNode j = m.getValue();
            System.out.println(str+s);
            if(j.isContainerNode()){
                getNext(j,str+"-");
            }
            if(j.isArray()){
                for(JsonNode subj:j){
                    getNext(subj,str+"--");
                }
            }
        }
    }

完整代码如下

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import javax.swing.plaf.synth.SynthEditorPaneUI;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author zhaoming
 * @Date 2020/9/30 10:59
 * @Version 1.0
 */


public class main {

    public static void main(String[] args) throws IOException {
        String str = "{\n" +
                "    \"status\":0,\n" +
                "    \"result\":{\n" +
                "        \"location\":{\n" +
                "            \"country\":\"日本\",\n" +
                "            \"province\":\"东京都\",\n" +
                "            \"city\":\"目黑区\",\n" +
                "            \"name\":\"目黑区\",\n" +
                "            \"id\":\"JPN10041030001\"\n" +
                "        },\n" +
                "        \"now\":{\n" +
                "            \"temp\":13,\n" +
                "            \"feels_like\":11,\n" +
                "            \"rh\":52,\n" +
                "            \"wind_class\":\"2级\",\n" +
                "            \"wind_dir\":\"东风\",\n" +
                "            \"text\":\"阴\",\n" +
                "            \"uptime\":\"20200220150000\"\n" +
                "        },\n" +
                "        \"forecasts\":[\n" +
                "            {\n" +
                "                \"date\":\"2020-02-20\",\n" +
                "                \"week\":\"星期四\",\n" +
                "                \"high\":12,\n" +
                "                \"low\":12,\n" +
                "                \"wc_day\":\"<3级\",\n" +
                "                \"wc_night\":\"<3级\",\n" +
                "                \"wd_day\":\"东风\",\n" +
                "                \"wd_night\":\"东北风\",\n" +
                "                \"text_day\":\"阴\",\n" +
                "                \"text_night\":\"多云\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2020-02-21\",\n" +
                "                \"week\":\"星期五\",\n" +
                "                \"high\":13,\n" +
                "                \"low\":13,\n" +
                "                \"wc_day\":\"<3级\",\n" +
                "                \"wc_night\":\"<3级\",\n" +
                "                \"wd_day\":\"东南风\",\n" +
                "                \"wd_night\":\"东南风\",\n" +
                "                \"text_day\":\"多云\",\n" +
                "                \"text_night\":\"多云\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2020-02-22\",\n" +
                "                \"week\":\"星期六\",\n" +
                "                \"high\":16,\n" +
                "                \"low\":16,\n" +
                "                \"wc_day\":\"3~4级\",\n" +
                "                \"wc_night\":\"3~4级\",\n" +
                "                \"wd_day\":\"西南风\",\n" +
                "                \"wd_night\":\"西南风\",\n" +
                "                \"text_day\":\"多云\",\n" +
                "                \"text_night\":\"多云\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2020-02-23\",\n" +
                "                \"week\":\"星期日\",\n" +
                "                \"high\":15,\n" +
                "                \"low\":15,\n" +
                "                \"wc_day\":\"<3级\",\n" +
                "                \"wc_night\":\"<3级\",\n" +
                "                \"wd_day\":\"西北风\",\n" +
                "                \"wd_night\":\"北风\",\n" +
                "                \"text_day\":\"多云\",\n" +
                "                \"text_night\":\"多云\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2020-02-24\",\n" +
                "                \"week\":\"星期一\",\n" +
                "                \"high\":14,\n" +
                "                \"low\":14,\n" +
                "                \"wc_day\":\"<3级\",\n" +
                "                \"wc_night\":\"<3级\",\n" +
                "                \"wd_day\":\"南风\",\n" +
                "                \"wd_night\":\"南风\",\n" +
                "                \"text_day\":\"多云\",\n" +
                "                \"text_night\":\"多云\"\n" +
                "            }\n" +
                "        ],\n" +
                " \"forecast_hours\":[\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":14,\n" +
                "                \"wind_class\":\"3~4级\",\n" +
                "                \"wind_dir\":\"西南风\",\n" +
                "                \"rh\":15,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":10,\n" +
                "                \"data_time\":\"2020-04-01 16:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":14,\n" +
                "                \"wind_class\":\"3~4级\",\n" +
                "                \"wind_dir\":\"西南风\",\n" +
                "                \"rh\":13,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":10,\n" +
                "                \"data_time\":\"2020-04-01 17:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":13,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西南风\",\n" +
                "                \"rh\":14,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":10,\n" +
                "                \"data_time\":\"2020-04-01 18:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":11,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西南风\",\n" +
                "                \"rh\":15,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":10,\n" +
                "                \"data_time\":\"2020-04-01 19:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":10,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西南风\",\n" +
                "                \"rh\":16,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":10,\n" +
                "                \"data_time\":\"2020-04-01 20:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":9,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西风\",\n" +
                "                \"rh\":18,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":6,\n" +
                "                \"data_time\":\"2020-04-01 21:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":9,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西风\",\n" +
                "                \"rh\":20,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":3,\n" +
                "                \"data_time\":\"2020-04-01 22:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":8,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西风\",\n" +
                "                \"rh\":21,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-01 23:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":7,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":26,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 00:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":6,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":31,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 01:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":6,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":36,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 02:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":5,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":39,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 03:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":4,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":42,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 04:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":4,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":45,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 05:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":5,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":40,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 06:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":7,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":34,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 07:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":8,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":29,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 08:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":11,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":29,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 09:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":13,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":29,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 10:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":16,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":29,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 11:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":17,\n" +
                "                \"wind_class\":\"3~4级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":24,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 12:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":18,\n" +
                "                \"wind_class\":\"3~4级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":19,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 13:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":19,\n" +
                "                \"wind_class\":\"3~4级\",\n" +
                "                \"wind_dir\":\"西北风\",\n" +
                "                \"rh\":14,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 14:00:00\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"text\":\"晴\",\n" +
                "                \"temp_fc\":19,\n" +
                "                \"wind_class\":\"<3级\",\n" +
                "                \"wind_dir\":\"西风\",\n" +
                "                \"rh\":17,\n" +
                "                \"prec_1h\":0,\n" +
                "                \"clouds\":0,\n" +
                "                \"data_time\":\"2020-04-02 15:00:00\"\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"message\":\"success\"\n" +
                "}\n";
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);
        try {
            JsonNode rootNode = mapper.readTree(str);
            getNext(rootNode,"");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void getNext(JsonNode jsonNode,String str){
        for(Iterator<Map.Entry<String,JsonNode>> it = jsonNode.fields(); it.hasNext();){
            Map.Entry<String,JsonNode> m = it.next();
            String s = m.getKey();
            JsonNode j = m.getValue();
            System.out.println(str+s);
            if(j.isContainerNode()){
                getNext(j,str+"-");
            }
            if(j.isArray()){
                for(JsonNode subj:j){
                    getNext(subj,str+"--");
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值