Fastjson Object String Interchange method instance __js

Source: Internet
Author: User



* * 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); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.