您的位置:首页 > 编程语言 > Java开发

Java用Gson遍历json所有节点

2018-03-17 09:43 1436 查看
<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 ja = 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();}}
代码示例来源于自动化测试REST API工具Wisdom RESTClienthttps://github.com/Wisdom-Projects/rest-client
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Wisdom RESTClient