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?

Thanks

View Answers

November 5, 2017 at 4:14 AM

Hi,

You can use the com.google.gson.Gson class of Google Gson library.

Then use following method to covert it to JSON String:

String json = gsonFormatted.toJson(map);

Here is full example of the program:

package net.roseindia;

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

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

public class JsonFormattedOutput {

    public static void main(String[] args) {

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

        map.put("ProductID", "C0001");
        map.put("Name", "Mouse");
        map.put("Price", "250");

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

Here is the output of the program:

JSON String:

{
  "Price": "250",
  "ProductID": "C0001",
  "Name": "Mouse"
}

Thanks









Related Tutorials/Questions & Answers:
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... library. Then use following method to covert it to JSON String: String json
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... library. Then use following method to covert it to JSON String: String json
Advertisements
How to convert date to string in Java in yyyy-mm-dd format
to String in yyyy-mm-dd format. How to convert date to string in Java in yyyy...How to convert date to string in Java in yyyy-mm-dd format  Hi, I... it to yyyy-mm-dd String format. Here is Java example for converting String Date
How to convert string to date in java in yyyy-mm-dd format?
How to convert string to date in java in yyyy-mm-dd format?  Hi, I have a date in yyyy-mm-dd format. How to convert string to date in java in yyyy... to format string to date. Here is the full example code: package net.roseindia
How to parse data in JSON format
How to parse data in JSON format  Hi, I want to parse following json string in Python and get values from it. user = {"user":"testuser", "password":"testpass", "isactive":"Yes"} How to parse data in JSON format? Thanks
Display String in Circular Format in Java
Display String in Circular Format in Java In this section, you will learn how to display string in a circular format.For this, we have specified the string 'WORD'. Now we have to shift each character of the string in the circular format
How to format number in Java?
How to format number in Java?  Hi, What is the best way to format a number in Java? How to format number in Java? Thanks   Hi, To format number in Java you can use the class java.text.NumberFormat. Here is simple
how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object...(String[] args) { Date date=new Date(); SimpleDateFormat sdf1
how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object...(String[] args) { Date date=new Date(); SimpleDateFormat sdf1
String to Date format
String to Date format  My Date format is String Eg(Jan 20 2011 ).... How to Change This values Like to database date format Eg(2011-01-20) using java   import java.util.*; import java.text.*; class ChangeDateFormat
String to date with no format
. but i want to write a java program to find out dis format for me. the result...String to date with no format  Hii.. I want to convert a string into a date .. this is simple .. but i want to do it without knowing the date format
How to convert EBCDIC format value into ASCII format value in java
How to convert EBCDIC format value into ASCII format value in java  how to convert EBCDIC data format into ASCII format data using java Use Case:I my file contains ASCII format values as well as EBCDIC format values.I need
ModuleNotFoundError: No module named 'json-format'
'json-format' How to remove the ModuleNotFoundError: No module named 'json-format' error? Thanks   Hi, In your python environment...ModuleNotFoundError: No module named 'json-format'  Hi, My Python
How to split string in Java?
How to split string in Java?  Hi, I have a string seperated by ',' comma. How to split the string? Thanks
How to Append String In Java?
How to Append String In Java?  How to write program in in Java for appending to a String? How to Append String In Java? What is the best way to do... the StringBuilder class. Check the example at Appending String in java. You can
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
Java convert string to date from format yyyy-mm-dd hh mm ss
Java convert string to date from format yyyy-mm-dd hh mm ss  Hi, I have date in the string yyyy-mm-dd hh mm ss format. Now I will want this string date into the date format. How to convert string to date from format yyyy-mm-dd
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... format. * This also shows you how to convert the UTC String format
How to append String in Java?
How to append String in Java?  Hi, I have a number of string in Java which is to be appended efficiently. How to append String in Java? Thanks...: str.append("India"); You check complete tutorial at Appending String in Java
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 format number in Java?
Learn about the code of formatting the number in Java This tutorial explains you how to format the number in Java. There are number of situations where we...: Decimal Format Example Check the more example of Java Beginners
ModuleNotFoundError: No module named 'oem-format-json'
named 'oem-format-json' How to remove the ModuleNotFoundError: No module named 'oem-format-json' error? Thanks   Hi, In your python...ModuleNotFoundError: No module named 'oem-format-json'  Hi, My
ModuleNotFoundError: No module named 'oem-format-minimize-json'
: No module named 'oem-format-minimize-json' How to remove the ModuleNotFoundError: No module named 'oem-format-minimize-json' error? Thanks  ...ModuleNotFoundError: No module named 'oem-format-minimize-json'  Hi
ModuleNotFoundError: No module named 'pretty-format-json'
named 'pretty-format-json' How to remove the ModuleNotFoundError: No module named 'pretty-format-json' error? Thanks   Hi, In your...ModuleNotFoundError: No module named 'pretty-format-json'  Hi, My
How to format text file? - Java Beginners
How to format text file?  I want the answer for following Query ***(Java code) How to open,read and format text file(notepad) or MS-word document in the same directory that of this required java program????  Hi
mockmvc post json string
mockmvc post json string  Hi, I am writing Spring Test cases. I want to post json to webservice. How to post post json string in Spring testing... post json string instead of regular parameters. Following code can be used
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 convert current date to dd mm yyyy format in Java?
How to convert current date to dd mm yyyy format in Java?  Hi, How to convert current date to dd mm yyyy format in Java? In my program I have to display the current date in the dd-mm-yyyy format to user on the website. How
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 get length of string in Java?
How to get length of string in Java?  What is the method of String for getting the length? How to get length of string in Java? Tell me code fast. Thanks   Hi, You may use the following code: String str= "Good
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 format current date into yyyy-mm-dd in Java
to do this in Java Programming Language. How to format current date...How to format current date into yyyy-mm-dd in Java  Hi, I have requirement to format the date into yyyy-MM-dd format for inserting it into MySQL
How To Append String In Java
How To Append String In Java In this section we will read about how to append String in Java. Here we will see the solution of this question through... which will demonstrate you about how to concatenate/append string in Java
How to convert current date to mm dd yyyy format in Java?
How to convert current date to mm dd yyyy format in Java?  Hi, How to convert current date to mm dd yyyy format in Java? Share me the program... for converting current date into MM-dd-yyyy format. Here is full code: package
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 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
how to covert JPG format to Binary formart using java code..
how to covert JPG format to Binary formart using java code..  convert JPG format to Binary formart How can i convert JPG format to Binary format using java code plz help me out
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... successfully."); } catch (Exception ex) {} } public static void main(String
java format date
java format date  Hi, How can I format the date in the following... SimpleDateFormatExample { public static void main(String[] args...-MM-dd"); String formattedDate=sdf.format(date
How to print this Format exactly?
How to print this Format exactly?   * * * & reverse
what is the differnce between the number format and string format when creatign the table in oracle - JDBC
what is the differnce between the number format and string format when... the number format and string format when creatign the table in oracle. if i enter the number in string format,can i know the advantage if i enter the number
How to add a file in GZIP file format in Java programme.
How to add a file in GZIP file format in Java programme.  Hi please help me. How to add a file GZIP File format in Java program. If example... in Gzip file format. For details visit http://www.roseindia.net/java/examples
How to convert excel file int xml format in java
How to convert excel file int xml format in java  How to read excel file(xls) which is stored in any directory(D://) and convert it into xml format in java and place in any directory(E://).Can any body provide the code

Ads