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
相关文章推荐
- Java用Gson遍历json所有节点
- Java用Gson遍历json所有节点
- Java用Jackson遍历json所有节点
- Java用Gson按照键值key排序json所有节点
- Java用Jackson遍历json所有节点
- Java用Gson按照键值key排序json所有节点
- Java用Jackson遍历json所有节点
- Java用Gson按照键值key排序json所有节点
- java 遍历所有子节点
- XPath遍历输出XML所有叶子节点
- Gson--Json串生成Java对象
- 第十二周 项目4 利用遍历思想求解图问题(输出通过一个节点的所有简单回路 )
- Java使用Dom4J 遍历复杂XML节点
- Java json序列化库gson(2)
- Java遍历一个目录下的所有文件
- JAVA 遍历文件夹下的所有文件(递归调用)
- 使用GSon根据节点名称读取json数组
- Gson在java对象和json字符串之间转换
- 翻译之使用Gson将java对象转换为json对象
- 遍历json对象---Java