How to convert map to json in Java

Ads
 

How to convert map to json in Java

Hi,

I my program I have an object of HashMap and I have to output this into json string.

How to convert map to json in Java?

Thanks

View Answers

November 5, 2017 at 3:56 AM

Hi,

You have to use the Google Gson library. Include following code into your pom.xml file:

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>

Google Gson library provides Gson class which can be used to format hashmap to JSON string.

It provides the method toJson(map) to converting object into json.

Here is complete example:

package net.roseindia;

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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class MapToJSONExample {

    public static void main(String[] args) {

        Map<String,String> map = new HashMap<String,String>();

        map.put("author", "Deepak Kumar");
        map.put("address", "Delhi, India");
        map.put("web", "https://www.roseindia.net");


        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        String jsonString = gson.toJson(map);

        System.out.println("JSON String not formatted:");
        System.out.println(jsonString);


        Gson gsonFormatted = new GsonBuilder().setPrettyPrinting().create();
        String json = gsonFormatted.toJson(map);
        System.out.println("JSON String formatted:");
        System.out.println(json);


    }

}

Thanks


November 5, 2017 at 3:57 AM

If you run the program it will give you output:

JSON String not formatted:

{"address":"Delhi, India","web":"https://www.roseindia.net",

"author":"Deepak Kumar"}

JSON String formatted:

{
  "address": "Delhi, India",
  "web": "https://www.roseindia.net",
  "author": "Deepak Kumar"
}

Thanks

Ads









Related Tutorials/Questions & Answers:
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
Advertisements
How to convert hashmap to json in Java?
the instruction in video titled "How to convert hashmap to json in Java?" where you can see the actual running example code. How to convert hashmap to json...://www.roseindia.net"); Video: How to convert hashmap to json in Java? Code
how to convert json into dataframe in scala
how to convert json into dataframe in scala  Hi, I have a JSON string and I want to convert it to dataframe in scala. how to convert json into dataframe in scala? I am reading some program and creating one line json and now I
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
How to convert string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
How to format json String in Java
How to format json String in Java  Hi, How to format json String in Java? I have an object of HashMap how to convert it to formatted output...(); String json = gsonFormatted.toJson(map); System.out.println("JSON String
How to format json String in Java
How to format json String in Java  Hi, How to format json String in Java? I have an object of HashMap how to convert it to formatted output...(); String json = gsonFormatted.toJson(map); System.out.println("JSON String
how to convert string to image in java
how to convert string to image in java  how to convert string to image in java? I know we need to convert image to a byte array at some point in my application but do not know how to convert a image into byte string. Please
How to convert Collection to Array in Java?
How to convert Collection to Array in Java?  Hello, I have a collection object in data with data in it. How to convert Collection to Array in Java...("This "); Above code create a collection and then add an item to it. Now to convert
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert String to int in Java
into the int variable. How to convert String to int in Java? Thanks   Hi, In Java you can convert String to in with the help of following two functions...How to convert String to int in Java  Hi, I have String with int
How to convert date to string in Java?
How to convert date to string in Java?  Hello developers, I have java.util.Date object and it has to be converted to date format. How to convert date to string in Java? Thanks   Hi, To convert java.util.Date object
How to convert http website into Https from java?
How to convert http website into Https from java?  could any one tell me how to convert http website into Https from java
How to convert this Java code into GUI?
How to convert this Java code into GUI?   import java.util.Scanner; public class StudentMarks { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks
How to convert Arraylist into String Array Java
How to convert Arraylist into String Array Java  Hi, I am beginners of Java programming. Can somebody Suggest me how to convert arraylist to string... information with example. You can find the online example of how to convert arraylist
How to Convert PDF into rtf File Java
How to Convert PDF into rtf File Java  Hi, How could in covert the PDF file to rtf file in Java Programming. Please suggest any online example... the online example of Java Convert PDF into rtf File. Thanks
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
How do you map Java Objects with Database tables?
How do you map Java Objects with Database tables?  Hi, How do you map Java Objects with Database tables? thanks
How to convert date to UTC format in Java?
How to convert date to UTC format in Java?  Hi, What is UTC Date format and How to convert date to UTC format in Java? I need simple to learn... of world. You can use Java program to convert the date in UTC String
how to convert java fram to java servlets
how to convert java fram to java servlets   hi every java master or java professional can you teach me how to convert JFram to Java Servlets program hope yours can reply me Thank you. below this is my own coding import
How to Convert SOAP Web service to REST Web service In Java ?
How to Convert SOAP Web service to REST Web service In Java ?  I want to Convert SOAP Web service(WSDL) to REST web service in JSON Format with GET...,and Jersey is a tool for REST. Please tell me how to convert SOAP web service
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive multiple files from a remote URL as for (String reportid : reportList...); } i want to convert the data from inputStream into Zip Stream and so

Ads