JSON例子一
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 gson = new Gson(); //第一个参数是一个Json数据 //第二个参数指定这个数据转换成哪个类的对象 User user = gson.fromJson(jsonData, User.class); System.out.println("name--->" + user.getName()); System.out.println("age--->" + user.getAge()); } }
完整代码见下载 _19_json02.rar
JSON例子二
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
/** * 但如果要把JSON数组转换成一系列的User对象,那要怎么做? * */ public class JsonUtils { public void pareseUserFromJson(String jsonData){ //此处TypeToken<LinkedList<User>>(){}为何有个大括号,因为他是个接口,但又没有要实现的类 Type listType = new TypeToken<LinkedList<User>>(){}.getType(); Gson gson = new Gson(); //LinkedList 增删操作较多而查询操作很少的情况下 LinkedList<User> users = gson.fromJson(jsonData,listType); for(Iterator iterator = users.iterator(); iterator.hasNext();){ User user = (User)iterator.next(); System.out.println("name--->" + user.getName()); System.out.println("age--->" + user.getAge()); } } }
完整代码见下载 _19_json03.rar