zoukankan      html  css  js  c++  java
  • Java用Jackson遍历json所有节点

    <!-- jackson begin -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.1.4</version>
    </dependency>
    <!-- jackson end -->
    
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
        public static void jsonLeaf(JsonNode node)
        {
            if (node.isValueNode())
            {
                System.out.println(node.toString());
                return;
            }
    
            if (node.isObject())
            {
                Iterator<Entry<String, JsonNode>> it = node.fields();
                while (it.hasNext())
                {
                    Entry<String, JsonNode> entry = it.next();
                    jsonLeaf(entry.getValue());
                }
            }
    
            if (node.isArray())
            {
                Iterator<JsonNode> it = node.iterator();
                while (it.hasNext())
                {
                    jsonLeaf(it.next());
                }
            }
        }
        
        public static void main(String[] args)
        {
            try
            {
                String json = FileUtils.readFileToString(new File("C://test.json"), "UTF-8");
                ObjectMapper jackson = new ObjectMapper();
                JsonNode node = jackson.readTree(txt);
                jsonLeaf(node);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    eclipse中不能识别enum
    The source attachment does not contain the source for the file Activity.class
    ASP.NET应用程序脱机问题
    鱼仔系统部署教程
    鱼仔系统开发教程
    mysql innodb cluster 无感知集群
    Mysql 8.0 新特性测试
    随笔1
    音频输出格式
    2012.02.03(S3C6410中文手册笔记)(一)
  • 原文地址:https://www.cnblogs.com/witpool/p/8444700.html
Copyright © 2011-2022 走看看