Friday , July 21 2017
Home / json / How to convert JSON to Java Map Object

How to convert JSON to Java Map Object

Here I am going to show you how to convert a JSON to Java Map object using Jackson.

JSON to Java Map :

In the previous tutorials we have seen how to convert a Java object to JSON string, you can see the dependencies of Jackson their.

Here I am going to create a JSON string and convert it into Java’s java.util.Map object.


package com.onlinetutorialspoint.json;

import java.util.HashMap;
import java.util.Map;

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

public class JSON_String_TO_Map {
    public static void main(String[] a) {

        ObjectMapper mapperObj = new ObjectMapper();
        Map<String,String> jsonMap = new HashMap<String,String>(); 
        try {
            jsonMap = mapperObj.readValue(jsonString(),
                    new TypeReference<HashMap<String,String>>(){});
            System.out.println(jsonMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String jsonString() {
        String str = "{\"studentId\": 1001, \"studentName\": \"Rajesh\","
                + "\"roleNumber\": \"106\",\"standard\": \"11th\"}";
        return str;

    }
}

{studentId=1001, standard=11th, studentName=Rajesh, roleNumber=106}

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

How to convert JSON to Java Object Example

Here I am going to show you how to convert JSON to Java Object using …

Leave a Reply

Your email address will not be published. Required fields are marked *