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

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 Python dictionary to JSON?
How to convert Python dictionary to JSON? Python example to create dictionary and convert to JSON pretty print format These days JSON is very popular... with the JSON data format. In this example I will show you how to instantiate Python
How do I convert a dictionary to a JSON object in Python?
How do I convert a dictionary to a JSON object in Python?  Hi, I... is returning the dictionary object. Now our requirement is to convert it to JSON... in the JSON document. I want to the easy way to convert the dictionary object
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 array to list in Java
How to Convert array to list in Java  Hi, I have object of array in Java and I want to convert it to list. How to Convert array to list in Java... your code for converting ArrayList to list in Java. Thanks
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 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 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 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
Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables
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 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 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 convert string to char array in Java
how to convert string to char array in Java  Hi, I have a string in Java which has to be converted to char array. What are the options to convert String object to char Array in Java? how to convert string to char array
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 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
how to convert java Applet to Jave Frame
how to convert java Applet to Jave Frame  hi every java master or Java Professional my name is vincent i'm java beginners hope u all can ,tech me how to convert Java Applet to Jave Frame below this code is my applet source code
How to hold data in map
How to hold data in map  Holding data in map: i have user1 and he has account1,account2,accoutn3 like that user2,user3 will have accounts. now how to hold users and their accounts in hashmap
How to convert binary to decimal in Java?
How to convert binary to decimal in Java program? In this section we are going to convert a binary number to it decimal representation... the function to convert the integer data into the binary to decimal. Here is the code
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
Convert dictionary to JSON Python
with two key and associated values. Now we want to convert it to JSON string..." } >>> The json.dumps() is used to convert dictionary object to JSON... to convert it to JSON string. If you want to sort the the data in created json
Convert dictionary to JSON Python
with two key and associated values. Now we want to convert it to JSON string..." } >>> The json.dumps() is used to convert dictionary object to JSON... to convert it to JSON string. If you want to sort the the data in created json
parse map in java
parse map in java  What is a Parser? Can you give the source code to parse map in Java
Java Map Example
Java Map Example  How we can use Map in java collection?   The Map interface maps unique keys to value means it associate value to unique...(String[] args) { Map mp = new HashMap(); mp.put(new Integer(2
ModuleNotFoundError: No module named 'convert-mapped-json'
named 'convert-mapped-json' How to remove the ModuleNotFoundError: No module named 'convert-mapped-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-mapped-json'  Hi, My
ModuleNotFoundError: No module named 'convert-csv-to-json'
named 'convert-csv-to-json' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-csv-to-json'  Hi, My
ModuleNotFoundError: No module named 'convert-csv-to-json'
named 'convert-csv-to-json' How to remove the ModuleNotFoundError: No module named 'convert-csv-to-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-csv-to-json'  Hi, My
ModuleNotFoundError: No module named 'convert-mapped-json'
named 'convert-mapped-json' How to remove the ModuleNotFoundError: No module named 'convert-mapped-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-mapped-json'  Hi, My
ModuleNotFoundError: No module named 'convert-mapped-json'
named 'convert-mapped-json' How to remove the ModuleNotFoundError: No module named 'convert-mapped-json' error? Thanks   Hi...ModuleNotFoundError: No module named 'convert-mapped-json'  Hi, My
How to convert String into java.util.Date
How to convert String into java.util.Date  Hi, How I can convert String date into java.util.Date in a Java program? Thanks   Hi... Getting time in Milliseconds Convert Date To Calendar How to convert String Date
How to Convert String to Date?
How to Convert String to Date?  Hi, I am new in Java and learning the Date manipulation in Java. I have to write a program in Java which converts... have to convert it to Date object. Tell me How to Convert String to Date? Thanks

Ads