FastJson - How to convert Java object to JSON and JSON to Java object

In this section, we will show you how to convert Java object to JSON and JSON to Java object with FastJson.

Note


Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Note


JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging.

JSON is built on two structures: 

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. 
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

1. Download FastJson 

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.19</version>
</dependency>

2. JSON to Java Objects

A User POJO, later uses this for conversion.

User.java

public class User {

private String name;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}


Main.java

import com.alibaba.fastjson.JSON;

public class Main {

public static void main(String[] args) {

String json = "{\"name\":\"sibin\", " +
"\"email\":\"sibin@gmail.in\"}";
User user = JSON.parseObject(json, User.class);

System.out.println(user);

}
}

Console Output:
User{name='sibin', email='sibin@gmail.in'}

3. Java Objects to JSON

Main.java

import com.alibaba.fastjson.JSON;

public class Main {

public static void main(String[] args) {

User user = new User();

user.setName("Sibin");
user.setEmail("sibin@gmail.in");

// Java objects to JSON
String json = JSON.toJSONString(user);
System.out.println(json);

// Java objects to JSON, pretty-print
String json2 = JSON.toJSONString(user, true);
System.out.println(json2);

}
}

Console Output:
{"email":"sibin@gmail.in","name":"Sibin"}
{
	"email":"sibin@gmail.in",
	"name":"Sibin"
}

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete