Home > Java > javaTutorial > In Java, how can we serialize a list of objects using flexjson?

In Java, how can we serialize a list of objects using flexjson?

WBOY
Release: 2023-09-05 23:09:06
forward
710 people have browsed it

In Java, how can we serialize a list of objects using flexjson?

Flexjson is a lightweight library for serialization and deserializationJava Objects > and come from JSON format. We can serialize the object list using the serialize() method of the JSONSerializer class. This method can perform shallow serialization on the target instance. We need to pass a list of objects of type List as a parameter to the serialize() method.

Syntax

public String serialize(Object target)
Copy after login

Example

import flexjson.JSONSerializer;
import java.util.*;
public class JsonSerializeListTest {
   public static void main(String[] args) {
      JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON
      Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad");
      Student s2 = new Student("Suresh", "Kumar", 30, "Chennai");
      Student s3 = new Student("Surya", "Krishna", 35, "Pune");
      List<Student> students = Arrays.asList(s1, s2, s3);
      String jsonStr = serializer.serialize(students);
      System.out.println(jsonStr);
   }
}
// Student class<strong>
</strong>class Student {
   private String firstName;
   private String lastName;
   private int age;
   private String address;
   public Student() {}
   public Student(String firstName, String lastName, int age, String address) {
      super();
      this.firstName = firstName;
      this.lastName = lastName;
      this.age = age;
      this.address = address;
   }
   public String getFirstName() {
      return firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public int getAge() {
      return age;
   }
   public String getAddress() {
     return address;
   }
   public String toString() {
      return "Student[ " +
      "firstName = " + firstName +
      ", lastName = " + lastName +
      ", age = " + age +
      ", address = " + address +
      " ]";
   }
}
Copy after login

Output

[
 {
    "address": "Hyderabad",
    "age": 28,
    "class": "Student",
    "firstName": "Raja",
    "lastName": "Ramesh"
 },
 {
    "address": "Chennai",
    "age": 30,
    "class": "Student",
    "firstName": "Suresh",
    "lastName": "Kumar"
 },
 {
    "address": "Pune",
    "age": 35,
    "class": "Student",
    "firstName": "Surya",
    "lastName": "Krishna"
 }
]
Copy after login

The above is the detailed content of In Java, how can we serialize a list of objects using flexjson?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template