Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 12 October 2013

JSON Tree Model in Jackson


In this Tutorials we are going to see how to read and write JSON Tree model using Jackson JsonNode.
The JsonNode is used to read and write tree structured date.It is similar to XML Dom tree.
1. Create project called JSONExample.
2. Create package called com.javatutorialscorner.jackson 
3. add jackson-mapper-asl-1.9.13.jar, jackson-core-asl-1.9.13.jar into build path.
4. Create java class called JacksonTreeExample under com.javatutorialscorner.jackson
JacksonTreeExample.java
01.package com.javatutorialscorner.jackson;
02. 
03.import java.io.File;
04.import java.io.IOException;
05.import java.util.Iterator;
06. 
07.import org.codehaus.jackson.JsonNode;
08.import org.codehaus.jackson.JsonProcessingException;
09.import org.codehaus.jackson.map.ObjectMapper;
10.import org.codehaus.jackson.node.ObjectNode;
11. 
12.public class JacksonTreeExample {
13. 
14. /**
15.  * @param args
16.  */
17. public static void main(String[] args) {
18.  // TODO Auto-generated method stub
19.  ObjectMapper mapper = null;
20. 
21.  mapper = new ObjectMapper();
22. 
23.  try {
24.   JsonNode root = mapper.readTree(new File(
25.     "C:\\jtc\\javatutorialscorner.json"));
26. 
27.   JsonNode id = root.path("id");
28.   System.out.println("ID : " + id.getIntValue());
29.   JsonNode name = root.path("name");
30.   System.out.println("Name : " + name.getTextValue());
31.   JsonNode list = root.path("list");
32.   Iterator<JsonNode> iterator = list.getElements();
33.   while (iterator.hasNext()) {
34.    JsonNode format = iterator.next();
35.    System.out.println("Format : " + format.getTextValue());
36.   }
37. 
38.   ((ObjectNode) root).put("id", 7);
39.   ((ObjectNode) root).put("name", "Dhoni");
40.   mapper.writeValue(new File("C:\\jtc\\javatutorialscorner.json"),
41.     root);
42.  } catch (JsonProcessingException e) {
43.   // TODO Auto-generated catch block
44.   e.printStackTrace();
45.  } catch (IOException e) {
46.   // TODO Auto-generated catch block
47.   e.printStackTrace();
48.  }
49. 
50. }
51. 
52.}

The javatutorialscorner.json file contains following String

{"id":10,"name":"Tendulkar","list":["Test - 198 ","ODI - 463 ","T20 - 1","IPL - 78"]}

Now run the program see the following output in console.

ID : 10

Name : Tendulkar

Format : Test - 198

Format : ODI - 463

Format : T20 - 1

Format : IPL - 78

and also javatutorialscorner.json File content replace with following contents.

{"id":7,"name":"Dhoni","list":["Test - 198 ","ODI - 463 ","T20 - 1","IPL - 78"]}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Create Map to JSON using JacksonIn this tutorial we are going to see how to create JSON from map and print json as string and also write json in file 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  … Read More
  • Jackson Streaming API to Write JSONIn this tutorial we are going to see how to write JSON into file using JsonGenerator class available in Jackson.Jackson Streaming API is high-performance Java API for read write JSON. JsonGenerator – is used to write JSON a… Read More
  • JSON Tree Model in JacksonIn this Tutorials we are going to see how to read and write JSON Tree model using Jackson JsonNode. The JsonNode is used to read and write tree structured date.It is similar to XML Dom tree. 1. Create project called JSONE… Read More
  • JSON to Map using JacksonIn this tutorial we are going to see how to convert JSON into Map and retrieve the values   1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. add jackson-map… Read More
  • How to Create Pretty Print JSON using JacksonIn this Tutorials we are going to see how to create Pretty Print JSON using Jackson. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. Create java class called… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JSON Tree Model in Jackson Rating: 5 Reviewed By: eHowToNow