JSON Example 1
Core code in mainactivity. Java
Private string jsondata = "{\" Name \ ": \" Michael \ ", \" Age \ ": 20 }";
Mainactivity. Java
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.view.View.OnClickListener; 5 import android.widget.Button; 6 7 public class MainActivity extends Activity { 8 private Button button = null; 9 private String jsonData = "{\"name\":\"Michael\",\"age\":20}";10 11 public void onCreate(Bundle savedInstanceState) {12 super.onCreate(savedInstanceState);13 setContentView(R.layout.main);14 button = (Button)findViewById(R.id.buttonId);15 button.setOnClickListener(new ButtonListener());16 }17 18 private class ButtonListener implements OnClickListener{ 19 public void onClick(View v){20 JsonUtils jsonUtils = new JsonUtils();21 jsonUtils.pareseUserFromJson(jsonData);22 }23 }24 25 26 }
User. Java
1 public class User { 2 private String name; 3 private String age; 4 5 public String getName() { 6 return name; 7 } 8 public void setName(String name) { 9 this.name = name;10 }11 public String getAge() {12 return age;13 }14 public void setAge(String age) {15 this.age = age;16 }17 }
Jsonutils. Java
Import COM. google. gson. gson; public class jsonutils {public void pareseuserfromjson (string jsondata) {gson = new gson (); // The first parameter is a JSON data // The second parameter specifies the class object to which the data is converted: User user = gson. fromjson (jsondata, user. class); system. out. println ("name --->" + User. getname (); system. out. println ("age --->" + User. getage ());}}
For the complete code, see download _19_json02.rar.
JSON Example 2
Core code in mainactivity. Java
Private string jsondata = "[{\" Name \ ": \" Michael \ ", \" Age \ ": 20 },{ \" Name \": \ "Mike \", \ "Age \": 21}] ";
Mainactivity. Java
1 public class MainActivity extends Activity { 2 private Button button = null; 3 private String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]"; 4 5 public void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.main); 8 button = (Button)findViewById(R.id.buttonId); 9 button.setOnClickListener(new ButtonListener());10 }11 12 private class ButtonListener implements OnClickListener{ 13 public void onClick(View v){14 JsonUtils jsonUtils = new JsonUtils();15 jsonUtils.pareseUserFromJson(jsonData);16 }17 }18 19 20 }
User. Java
1 public class User { 2 private String name; 3 private String age; 4 5 public String getName() { 6 return name; 7 } 8 public void setName(String name) { 9 this.name = name;10 }11 public String getAge() {12 return age;13 }14 public void setAge(String age) {15 this.age = age;16 }17 }
Jsonutils. Java
/*** What should I do if I want to convert a JSON array into a series of user objects? **/Public class jsonutils {public void pareseuserfromjson (string jsondata) {// here typetoken <shortlist <user> () {} contains braces because it is an interface, but there is no class type listtype = new typetoken <shortlist <user >> (){}. getType (); gson = new gson (); // when there are many Add/delete operations on the shortlist and few query operations, the shortlist <user> Users = gson. fromjson (jsondata, listtype); For (iterator = users. iterator (); iterator. hasnext ();) {user = (User) iterator. next (); system. out. println ("name --->" + User. getname (); system. out. println ("age --->" + User. getage ());}}}
For the complete code, see download _19_json03.rar.