FastJson - Convert JSON Array String to List and List to JSON Array String

In this section, we will show you how to convert JSON Array String to List and List to JSON Array String 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>

1. Convert JSON Array String to List

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;
import java.util.List;

public class Main {

public static void main(String[] args) {

String json = "[{\"name\":\"sibin\", " +
"\"email\":\"sibin@gmail.in\"}," +
" {\"name\":\"tom\", " +
"\"email\":\"tom@gmail.in\"}]";
List<User> list = JSON.parseArray(json, User.class);

System.out.println(list);

}
}

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

2. List to JSON Array String

Main.java

import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;

public class Main {

public static void main(String[] args) {

User user1=new User();
user1.setEmail("sibin@gmail.in");
user1.setName("sibin");

User user2=new User();
user2.setEmail("tom@gmail.in");
user2.setName("tom");

List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);

String json = JSON.toJSONString(users);
System.out.println(json);

}
}


Console Output:
[{"email":"sibin@gmail.in","name":"sibin"},{"email":"tom@gmail.in","name":"tom"}]

More related topics,

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

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