jackJson的基本用法3___树形读写

来源:互联网 发布:python 入门教程pdf 编辑:程序博客网 时间:2024/06/16 17:00

1.添加依赖

<!-- jackson framework --><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.8.5</version></dependency>

2.我们可以像操作html或者xml那样读写json

@Testpublic void Test4() {try {BufferedReader file = new  BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\java\\User.json"));ObjectMapper map = new ObjectMapper();JsonNode jsonnode = map.readTree(file);//指定路劲的nodeJsonNode names = jsonnode.path("name");//读取指定路径的数据String na = names.getTextValue();System.out.println(na);JsonNode mess = jsonnode.path("messages"); Iterator<JsonNode> itr = mess.getElements();while(itr.hasNext()){System.out.println(itr.next().getTextValue());}//更新((ObjectNode)jsonnode).put("name","update");((ObjectNode)jsonnode).remove("age");map.writeValue(new File("C:\\Users\\Administrator\\Desktop\\java\\User.json"), jsonnode);//} catch (FileNotFoundException e) {e.printStackTrace();} catch (JsonProcessingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


0 0