Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Jackson Tree Model example

In this example we are going to see how to use Jackson Tree Model. This model is very similar in many ways with the DOM representation. We are going to see how to create the tree model and update the JSON object with new values.
 
 
 
 
 
 
 
 
 
Here is the file we are going to use for the demo:

mapExample.json:

{"names":["Jonh","Jack","James"],"interest":"Java","domain":"JavaCodeGeeks.com","members":400}

JacksonTreeNodeTutorial.java:

package com.javacodegeeks.java.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;

public class JacksonTreeNodeTutorial {

	private static final String jsonFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\mapExample.json";

	public static void main(String[] args) {

		ObjectMapper mapper = new ObjectMapper();

		try {

			FileReader fileReader = new FileReader(jsonFilePath);

			BufferedReader bufferedReader = new BufferedReader(fileReader);

			JsonNode rootNode = mapper.readTree(bufferedReader);

			JsonNode domainNode = rootNode.path("domain");

			System.out.println("domain : "+domainNode.getTextValue());

			JsonNode interestNode = rootNode.path("interest");

			System.out.println("interest : " + interestNode.getTextValue());

			JsonNode memebers = rootNode.path("members");

			System.out.println("members : "+memebers.getIntValue());

			JsonNode namesListNode = rootNode.path("names");

			System.out.println("names : ");

			Iterator iterator = namesListNode.getElements();

			while (iterator.hasNext()) {

				JsonNode temp = iterator.next();
				System.out.println(" "+temp.getTextValue());

			}

			((ObjectNode)rootNode).put("domain", "wwww.javacodegeeks.com");

			((ObjectNode)rootNode).put("members", 600);

			((ObjectNode)rootNode).remove("interest");

			File file = new File(jsonFilePath);

			mapper.writeValue(file, rootNode);

		} catch (JsonGenerationException e) {

			e.printStackTrace();

		} catch (JsonMappingException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}
}

mapExample.json:

{"names":["Jonh","Jack","James"],"domain":"wwww.javacodegeeks.com","members":600}

 
This was an example on Jackson Tree Model.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books