Moshi - 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 Moshi in Java.

Note


Moshi is a modern JSON library for Java that will give us powerful JSON serialization and deserialization in our code with little effort.

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 Moshi

<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi-adapters</artifactId>
<version>1.13.0</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.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

public class Main {

public static void main(String[] args) throws IOException {

String json = "[{\"name\":\"sibin\", " +
"\"email\":\"sibin@gmail.in\"}," +
" {\"name\":\"tom\", " +
"\"email\":\"tom@gmail.in\"}]";

Moshi moshi = new Moshi.Builder().build();
Type type = Types.
newParameterizedType(List.class, User.class);
JsonAdapter<List<User>> jsonAdapter = moshi.adapter(type);
List<User> list = jsonAdapter.fromJson(json);
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.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class Main {

public static void main(String[] args) throws IOException {

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);

Moshi moshi = new Moshi.Builder().build();
Type type = Types.
newParameterizedType(List.class, User.class);
JsonAdapter<List<User>> jsonAdapter = moshi.adapter(type);
String json = jsonAdapter.toJson(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