* * File name: Fastjsontest.java * Copyright: Newegg Technologies Co., Ltd. Copyright YYYY-YYYY, all rights reserved * Description: < description > * Modified by: GC57 * Modified Time: 2014-10-23 * Tracking Number: < Tracking number > * Amendment Number: < modification number > * Modified content: <
Modify Content > * * Import java.util.ArrayList;
Import java.util.List;
Import Com.alibaba.fastjson.JSON;
Class User {private String name;
private int age;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
public int getage () {return age;
public void Setage (int age) {this.age = age;
@Override public String toString () {return "User [name=" + name + ", age=" + Age + "]";
}
};
Class UserGroup {private String name;
Private list<user> users = new arraylist<user> ();
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
Public list<user> Getusers () {return users; } public void Setusers (list<uSer> users) {this.users = users;
@Override public String toString () {return "usergroup [name= + name +", users= "+ users +]";
} public class Fastjsontest {public static void main (string[] args) {//Build users geust user Guestuser = new username ();
Guestuser.setname ("Guest");
Guestuser.setage (28);
The build user root user Rootuser = new user ();
Rootuser.setname ("root");
Guestuser.setage (35);
Build user Group Object UserGroup Group = new UserGroup ();
Group.setname ("admin");
Group.getusers (). Add (Guestuser);
Group.getusers (). Add (Rootuser);
User group object to JSON string jsonstring = json.tojsonstring (group);
System.out.println ("jsonstring:" + jsonstring);
JSON String User Group object UserGroup group2 = Json.parseobject (jsonstring, Usergroup.class);
System.out.println ("group2:" + group2);
Build user Object array user[] users = new user[2];
Users[0] = Guestuser;
USERS[1] = Rootuser;
User Object array to JSON string jsonString2 = json.tojsonstring (users); System.out.println ("JsonString2:" + JSONSTRING2);
JSON string User Object list list<user> users2 = Json.parsearray (jsonString2, User.class);
System.out.println ("users2:" + users2); }
}