您好,欢迎访问一九零五行业门户网

我们如何使用Java中的flexjson将对象数组序列化?

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.
syntaxpublic string serialize(object target)
in the below program, we need to serialize an array of objects.
exampleimport 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 classclass 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 + " ]"; }}
输出[ { "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" }]
以上就是我们如何使用java中的flexjson将对象数组序列化?的详细内容。
其它类似信息

推荐信息