In this quick article, we show you how to serialize and deserialize an array or a multidimensional array to and from its JSON representation.
GSON Maven Dependency
To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
Serializing and Deserializing Integer Array
This example serializes and deserializes an Integer Array to and from JSON representation.
Gson gson = new Gson();
int[] marks = { 100, 90, 85 };
System.out.print("marks:" + gson.toJson(marks) + ",");
// De-serialization
marks = gson.fromJson("[100,90,85]", int[].class);
System.out.println("marks:" + Arrays.toString(marks));
Output:
marks:[100,90,85],marks:[100, 90, 85]
Serializing and Deserializing String Array
This example serializes and deserializes a String Array to and from JSON representation.
Gson gson = new Gson();
String[] names = { "Ram", "Shyam", "Mohan" };
// serialization
System.out.print("names:" + gson.toJson(names));
System.out.println("}");
// De-serialization
names = gson.fromJson("[\"Ram\",\"Shyam\",\"Mohan\"]", String[].class);
System.out.println("names:" + Arrays.toString(names));
Output:
names:["Ram","Shyam","Mohan"]}
names:[Ram, Shyam, Mohan]
Serializing and Deserializing Custom Array of user objects
This example serializes and deserializes a User Array to and from JSON representation. Let's first create User POJO class:
package net.javaguides.gson;
public class User {
private transient int id;
private String firstName;
private String lastName;
private int age;
private String gender;
private String password;
public int getId() {
return id;
}
public void setId(int i) {
this.id = i;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", gender=" +
gender + "]";
}
}
package net.javaguides.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GSONArrayExamples {
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
User[] users = new User[2];
User user = new User();
user.setFirstName("Ramesh");
user.setLastName("Fadatare");
user.setGender("Male");
user.setAge(28);
user.setId(100);
User user1 = new User();
user1.setFirstName("John");
user1.setLastName("Cena");
user1.setGender("Male");
user1.setAge(50);
user1.setId(101);
users[0] = user;
users[1] = user1;
// Serialization users
System.out.println(gson.toJson(users));
String usersJsonArray = "[\r\n" +
" {\r\n" +
" \"firstName\": \"Ramesh\",\r\n" +
" \"lastName\": \"Fadatare\",\r\n" +
" \"age\": 28,\r\n" +
" \"gender\": \"Male\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"John\",\r\n" +
" \"lastName\": \"Cena\",\r\n" +
" \"age\": 50,\r\n" +
" \"gender\": \"Male\"\r\n" +
" }\r\n" +
"]";
// De-Serialization users
System.out.println(gson.fromJson(usersJsonArray, User[].class));
}
}
Output:
[
{
"firstName": "Ramesh",
"lastName": "Fadatare",
"age": 28,
"gender": "Male"
},
{
"firstName": "John",
"lastName": "Cena",
"age": 50,
"gender": "Male"
}
]
[Lnet.javaguides.gson.User;@7aec35a
Serializing and Deserializing Multi-Dimensional Array
This example demonstrates how to serialize and deserialize a multi-dimensional array to and from JSON representation.
package net.javaguides.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GSONArrayExamples {
public static void main(String[] args) {
multiDimensionalArray();
}
private static void multiDimensionalArray() {
Gson gson = new GsonBuilder().create();
int[][] multiIntArray = new int[][] {
{
1,
2,
3
}, {
4,
5,
6
}
};
// serialization
gson.toJson(multiIntArray); // => [[1,2,3],[4,5,6]]
// deserialization
int[][] intArray2 = gson.fromJson("[[1,2,3],[4,5,6]]", int[][].class);
}
}
Comments
Post a Comment
Leave Comment