Thursday , May 25 2017
Home / json / How to convert Java Map to JSON

How to convert Java Map to JSON

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

Java Map to JSON :

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 Java’s java.util.Map object and convert it in to JSON string.


package com.onlinetutorialspoint.json;

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

import org.codehaus.jackson.map.ObjectMapper;

public class Java_Map_To_Json {
    public static void main(String[] args) {
        Map<String,String> scoreMap = new HashMap<String,String>(); 
        scoreMap.put("Dhoni", "102");
        scoreMap.put("Yuvraj", "94");
        scoreMap.put("Raina", "82");
        scoreMap.put("Kohli", "120");
        ObjectMapper mapperObj = new ObjectMapper();
        try {
            String jsonFormat = mapperObj.writeValueAsString(scoreMap);
            System.out.println(jsonFormat);    
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}

{“Kohli”:”120″,”Yuvraj”:”94″,”Dhoni”:”102″,”Raina”:”82″}

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+ ,

Check Also

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 *