Genson - 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 Genson.
Note
Genson is a complete json <-> java conversion library, providing full databinding, streaming and much more.
Gensons main strengths?
- Easy to use and just works!
- Its modular and configurable architecture.
- Speed and controlled small memory foot print making it scale.
Note
Genson is a complete json <-> java conversion library, providing full databinding, streaming and much more.
Gensons main strengths?
- Easy to use and just works!
- Its modular and configurable architecture.
- Speed and controlled small memory foot print making it scale.
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.
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 Genson
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.6</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.owlike.genson.GenericType;
import com.owlike.genson.Genson;
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 = new Genson().
deserialize(json,new GenericType<List<User>>(){} );
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.owlike.genson.Genson;
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 = new Genson().serialize(users);
System.out.println(json);
}
}
Console Output:
[{"email":"sibin@gmail.in","name":"sibin"},{"email":"tom@gmail.in","name":"tom"}]
More related topics,