Home > Java > javaTutorial > body text

How can we serialize array of objects using flexjson in Java?

王林
Release: 2023-08-26 09:21:21
forward
1344 people have browsed it

How can we serialize array of objects using flexjson in Java?

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can also serialize an array of objects using the serialize() method of JSONSerializer class, this performs a shallow serialization of the target instance.

Syntax

public String serialize(Object target)
Copy after login

In the below program, we need to serialize an array of objects.

Example

import flexjson.JSONSerializer;
public class JsonSerializeArrayTest {
   public static void main(String[] args) {
      JSONSerializer serializer = new JSONSerializer().prettyPrint(true);
      Student stud1 = new Student("Adithya", "Sai", 28, "Hyderabad");
      Student stud2 = new Student("Jai", "Dev", 30, "Chennai");
      Student stud3 = new Student("Ravi", "Chandra", 35, "Pune");
      Student[] students = {stud1, stud2, stud3};
      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

输出

[
 {
 "address": "Hyderabad",
 "age": 28,
 "class": "Student",
 "firstName": "Adithya",
 "lastName": "Sai"
 },
 {
 "address": "Chennai",
 "age": 30,
 "class": "Student",
 "firstName": "Jai",
 "lastName": "Dev"
 },
 {
 "address": "Pune",
 "age": 35,
 "class": "Student",
 "firstName": "Ravi",
 "lastName": "Chandra"
 }
]
Copy after login

The above is the detailed content of How can we serialize array of objects using flexjson in Java?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!