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