Java uses Gson to traverse all nodes in json

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
    public  static  void jsonTree(JsonElement e)
    {
        if (e.isJsonNull())
        {
            System.out.println(e.toString());
            return;
        }

        if (e.isJsonPrimitive())
        {
            System.out.println(e.toString());
            return;
        }

        if (e.isJsonArray())
        {
            JsonArray and = e.getAsJsonArray ();
            if ( null ! = ja)
            {
                for (JsonElement ae: ja)
                {
                    jsonTree(ae);
                }
            }
            return;
        }

        if (e.isJsonObject())
        {
            Set<Entry<String, JsonElement>> es = e.getAsJsonObject().entrySet();
            for (Entry<String, JsonElement> en : es)
            {
                jsonTree(en.getValue());
            }
        }
    }
    
    public static void main(String[] args)
    {
        try
        {
            String json = FileUtils.readFileToString(new File("C://test//test.txt"), "UTF-8");
            JsonParser p = new JsonParser();
            JsonElement e = p.parse(json);
            jsonTree(e);
        }
        catch(Exception e)
        {
            e.printStackTrace ();
        }
    }

The code example comes from the automated testing REST API tool Wisdom RESTClient
https://github.com/Wisdom-Projects/rest-client

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123486&siteId=291194637