Gson Analysis of Android development notes

Source: Internet
Author: User

In the last chapter we talked about the JSON parsing, we have found that parsing a simple JSON data has written so much code, if it is a complex and large JSON data, it must not write much.

So Google has launched a JSON parsing artifact---Gson.

What is the Gson for? It is used to convert JSON data into objects, or to convert objects into JSON data. You only need to import the appropriate jar package.

Gson:http://download.csdn.net

This time the JSON data is:

{name:张三 ,age:26,phone:[131,132],score:[{id:语文,fenshu:100},{id:数学,fenshu:60},{id:化学,fenshu:120}]}

Steps:

① Import the downloaded Gson jar package into the Libs directory

② Creating Gson Objects

Gson gson = new Gson();

③ Create a JavaBean to turn into

I create a Student class here

Student.java

the//student property must be the same as the key value in JSON. //jsonobject transformed into classes, jsonarray into sets Public classStudent { PublicString name; PublicString age; Publicarraylist<string> phone =NewArraylist<string> (); PublicArraylist<score> score =NewArraylist<score> (); Public classScore { PublicString ID; PublicString Fenshu; @Override PublicStringtoString() {return "ID:"+ ID +"Fenshu:"+ Fenshu; }} @Override PublicStringtoString() {return "Name:"+ name +"Age:"+ Age +"Phone:"+ Phone.Get(0)+","+ Phone.Get(1) +"Score:"+ score.tostring (); }}

Note:

1. The class name of the class that needs to be generated is defined by yourself and has no relation;

2. The attribute name inside the class must be identical to the key inside the JSON field;

3, internal nested in the [] part is a list

④ converting JSON data into student objects

Student student = gson.fromJson(JSONDATA, Student.class);

Effect:

Is it easier to use gson than the traditional method?

Core code:

Activity_main.xml

<relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:layout_width  = "match_parent"  android:layout_height  =" match_parent " tools:context  =" com.example.gson.MainActivity " >     <button  android:layout_ Width  = "match_parent"  android:layout_height< /span>= "wrap_content"  android:onclick  = "Gson"  android:text  = " Gson parsing "/>     <TextViewandroid:textsize="20sp"android:id="@+id/tv"  Android:layout_centerinparent="true"android:layout_width="Wrap_content"  Android:layout_height="wrap_content" />                                        </relativelayout>

Mainacitivity.java

 Public  class mainactivity extends Activity {    PrivateTextView TV;//json Data     Public Static FinalString Jsondata ="{name: Zhang San, age:26,phone:[131,132],score:[{id: Chinese, Fenshu:100},{id: Mathematics, Fenshu:60},{id: Chemistry, fenshu:120}]}";@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);    TV = (TextView) Findviewbyid (r.id.tv); } Public void Gson(View view) {//Create a Gson objectGson Gson =NewGson ();//Convert JSON data to student objectStudent Student = Gson.fromjson (Jsondata, Student.class);//The parsed JSON data is displayed on the TextViewTv.settext (Student.tostring ()); }}

Student.java

 Public classStudent { PublicString name; PublicString age; Publicarraylist<string> phone =NewArraylist<string> (); PublicArraylist<score> score =NewArraylist<score> (); Public classScore { PublicString ID; PublicString Fenshu; @Override PublicStringtoString() {return "ID:"+ ID +"Fenshu:"+ Fenshu; }} @Override PublicStringtoString() {return "Name:"+ name +"Age:"+ Age +"Phone:"+ Phone.Get(0)+","+ Phone.Get(1) +"Score:"+ score.tostring (); }}

Gson Analysis of Android development notes

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.