Monday 14, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Friday, 18 October 2013

JSON to Map using Jackson


In 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-mapper-asl-1.9.13.jar, jackson-core-asl-1.9.13.jar into build path.
4. Create java class called JSONToMapExample under com.javatutorialscorner.jackson
JSONToMapExample.java
package com.javatutorialscorner.jackson;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JSONToMapExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ObjectMapper mapper = null;

Map<String, Object> map = null;

ArrayList<String> tutorials = null;

try {
mapper = new ObjectMapper();

map = mapper.readValue(
new File("C:\\jtc\\javatutorialscorner.json"),
new TypeReference<Map<String, Object>>() {
});
System.out.println("Name : " + map.get("name"));
System.out.println("Rank : " + map.get("rank"));

tutorials = (ArrayList<String>) map.get("tutorials");

for (String tutorial : tutorials) {
System.out.println("Tutorial : " + tutorial);
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

Now run the program see the following output in console.

Name : Java Tutorials Corner

Rank : 1


Tutorial : Java


Tutorial : Servlet


Tutorial : JSP


Tutorial : Struts 1.x


Tutorial : Struts 2.x


Tutorial : Spring


Tutorial : Hibernate


Tutorial : Log4j


Tutorial : JSON


Tutorial : Web Services


Tutorial : JSF


Tutorial : Eclipse

Shop and help us

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

Related Posts:

  • String Concatenation in JavaFollowing example shows String concatenation in Java using + operator and StringBuffer.append() method. Sample Programpackage com.javatutorialcorner.javastring;public class StringConcatenation { public static void main(Str… Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • How to add value to all types of Collections Following example shows how to add values in all types collections in Java. Sample Program AddValueToCollection.java package com.javatutorialcorner.collection; import java.util.ArrayList; import java.util.Collection; impo… Read More
  • How to get the size of Collection Following example shows how to get the size of collection using collection.size() method in Java. int java.util.List.size()sizeint size() Returns the number of elements in this list. If this list contains more than Integer.… Read More
  • How to replace the element from List Following example shows how to replace all the occurrence of element with another element using Collections.replaceAll(list, oldVal, newVal) method in Java. <String> boolean java.util.Collections.replaceAll(List<St… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JSON to Map using Jackson Rating: 5 Reviewed By: eHowToNow