0% found this document useful (0 votes)
5 views3 pages

Convert Java into JSON and JSON into Java

Uploaded by

unnikallikattu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views3 pages

Convert Java into JSON and JSON into Java

Uploaded by

unnikallikattu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Convert Java into JSON and JSON into Java. All Possible Examples.

Learning how to convert Java objects into JSON and JSON into Java will help you a lot
in your work as a full stack mobile app developer. In this blog post I will share with you
code examples which will help you do most of the tasks you will even need to do when
working with JSON and Java. I will share with you:

 How to create a JSON object in Java


 How to create JSON object from a JSON String
 How to convert Java JSON object into a JSON String
 How to convert Java Object (POJO) into a JSON Object and a JSON String
 Convert JSON Array String in Java Array List
 How to convert Java objects like Map or List into JSON objects

I think the above is all you will most often be coming across with and the below code
examples are all you need to help you with your task.

Before you begin, you will need to either download Google GSON library or add the
below dependency into your pom.xml. At the time I am writing this blog post the GSON
library is of version 2.8.0.

GSON Dependency for pom.xml


<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>

Create a JSON object in Java


// Create new JSON Object
JsonObject person = new JsonObject();
person.addProperty("firstName", "Sergey");
person.addProperty("lastName", "Kargopolov");

System.out.println(person.toString());

Create Inner/Nested JSON Object


JsonObject person = new JsonObject();
person.addProperty("firstName", "Sergey");
person.addProperty("lastName", "Kargopolov");

// Create Inner JSON Object


JsonObject address = new JsonObject();
address.addProperty("country", "ru");
address.addProperty("city", "Moscow");

person.add("address", address);

System.out.println(person.toString());

Create JSON Array


// Create JSON Array
JsonArray languages = new JsonArray();
languages.add("Russian");
languages.add("English");
languages.add("French");

System.out.println(languages.toString());

Create JSON Array from String


// Create JSON Array from String
String jsonString = "{\"firstName\":\"Sergey\",\"lastName\":\"Kargopolov\"}";
JsonParser jsonParser = new JsonParser();
JsonObject objectFromString = jsonParser.parse(jsonString).getAsJsonObject();

System.out.println(objectFromString.toString());

Convert JSON Array String into JSON Array

Convert JSON Array String into JSON Array


JsonParser jsonParser = new JsonParser();

// Convert JSON Array String into JSON Array


String jsonArrayString = "[\"Russian\",\"English\",\"French\"]";
JsonArray arrayFromString =
jsonParser.parse(jsonArrayString).getAsJsonArray();

System.out.println(arrayFromString.toString())

System.out.println(arrayFromString.toString())

Convert JSON Array String into Java Array List

// Convert JSON Array String into Java Array List


String jsonArrayString = "[\"Russian\",\"English\",\"French\"]";
Gson googleJson = new Gson();
ArrayList javaArrayListFromGSON = googleJson.fromJson(arrayFromString,
ArrayList.class);

System.out.println(javaArrayListFromGSON);

Convert Java Plain Object into JSON


//Convert Java Plain Object into JSON
Person personPojo = new Person();
personPojo.setFirstName("Sergey");
personPojo.setLastName("Kargopolov");

Gson gsonBuilder = new GsonBuilder().create();


String jsonFromPojo = gsonBuilder.toJson(personPojo);

System.out.println(jsonFromPojo);

Convert Java Map into JSON


Gson gsonBuilder = new GsonBuilder().create();

// Convert Java Map into JSON


Map personMap = new HashMap();
personMap.put("firstName", "Sergey");
personMap.put("lastName", "Kargopolov");
String jsonFromJavaMap = gsonBuilder.toJson(personMap);

System.out.println(jsonFromJavaMap);

Convert Java ArrayList into JSON


Gson gsonBuilder = new GsonBuilder().create();
// Convert Java Array into JSON
List languagesArrayList = new ArrayList();
languagesArrayList.add("Russian");
languagesArrayList.add("English");
languagesArrayList.add("French");

String jsonFromJavaArrayList = gsonBuilder.toJson(languagesArrayList);

System.out.println(jsonFromJavaArrayList);

All the above code examples in one single file

You might also like