How Android uses Fastjson

Source: Internet
Author: User
Tags json tostring java se

Fastjson is a JSON-processing toolkit for Alibaba Open source, including "serialization" and "deserialization" in two parts.

Tests show that Fastjson has a very fast performance beyond any other Java Json parser. Includes self-proclaimed fastest Jackjson, powerful, fully supported Java beans, collections, maps, dates, enums, support paradigm, support introspection; no dependencies, able to run directly above the Java SE 5.0 version; Support Android; Open source (Apache 2.0)

Fastjson Description: Https://github.com/Alibaba/fastjson/wiki/%E9%A6%96%E9%A1%B5

Note: The Fastjson has a Java version and an Android version, so don't get it wrong, and many blogs don't mention it.

Android Fastjson jar Download Address: Https://repo1.maven.org/maven2/com/alibaba/fastjson/1.2.11/fastjson-1.2.11.jar

Copy the jar package to the project's Libs directory, and Android automatically references

Common methods in Fastjson:

1, Person.java

Class Person {//name private String name = NULL;
	Age private int = 0;
	is a male private Boolean Ismale = true;

	The child's name is private list<string> childname = new arraylist<string> ();
	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;
	public Boolean Ismale () {return ismale;
	} public void Setmale (Boolean ismale) {this.ismale = Ismale;
	Public list<string> Getchildname () {return childname;
	public void Setchildname (list<string> childname) {this.childname = ChildName;
		public void Init () {name = ' Bob ';
		Age = 25;
		Ismale = true;
		Childname.add ("Tina");
		Childname.add ("Linda");
	Childname.add ("Tom");  @Override public String toString () {//url:http://www.bianceng.cn/os/extra/201608/50411.htm string str = "Name: "+ Name +", Ages: "+ age +", Sex: "+ (Ismale?") Male ":" female ")+ "\ n";
			if (childname!= null && childname.size ()!= 0) {str = str + "Number of children:" + childname.size () + "\ n";
			for (int i=0;i<childname.size (); i++) {str = str + "\ t" + i + "." + childname.get (i) + "\ n";
	} return str; }
}

2, Activity_fastjson.xml on a textview to display the results

3, Fastjsonactivity.java

 public class Fastjsonactivity extends activity {public TextView TextView;
		@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
		
		Setcontentview (R.layout.activity_fastjson);
		
		TextView = (TextView) Findviewbyid (R.id.textview);
		person who = new person ();
		Person.init ();
		(1) Jsonobject personobject = (jsonobject) Json.tojson (person);
		AddText (Personobject.tostring ());
		(2) String personstr = json.tojsonstring (person);
		AddText (PERSONSTR);
		(3) person = Json.parseobject (personstr, Person.class);
		AddText (Person.tostring ());
		(4) String jsonarrstr = "[a\", \ "B\", "c\", \ "d\"] ";
		Jsonarray Jsonarr = Json.parsearray (JSONARRSTR);
		AddText (Jsonarr.tostring ());
		(5) list<string> strlist = Json.parsearray (Jsonarrstr, String.class);
	AddText (Strlist.tostring ());
	public void addtext (String str) {Textview.settext (Textview.gettext (). toString () + str + "\ n"); }
}

Output results:

{"ChildName": ["Tina", "Linda", "Tom"], "name": "Bob", "Age": "Ismale": true}

{"Age": "ChildName": ["Tina", "Linda", "Tom"], "Ismale": True, "name": "Bob"}

Name: Bob, Age: 25, Sex: Male

Number of children: 3

0. Tina

1. Linda

2. Tom

["A", "B", "C", "D"]

[A,b,c,d]

Related Article

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.