JSON数据的解析:解析JSON数据,首先需要明确待解析的是JSON Object还是JSON array,然后需要确定采用哪种解析技术. android平台上一般有2种解析技术可供选择: android内置的org.json包和google的开源gson库.以下将分别采用这两种技术解析JSON对象和JSON数组.
1. 采用android内置的org.json包解析JSON对象.假设待解析的JSON数据为json = "{\"name\":\"coolxing\", \"age\"=24, \"male\":true, \"address\":{\"street\":\"huiLongGuan\", \"city\":\"beijing\", \"country\":\"china\"}}",其中\用于转义表达式中的双引号.首先定义2个JavaBean:
Java代码
public class Address {
private String street;
private String city;
private String country;
public Address() {
super();
}
public Address(String street, String city, String country) {
super();
this.street = street;
this.city = city;
this.country = country;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address [street=" + street + ", city=" + city + ", country="
+ country + "]";
}
}
public class Address {
private String street;
private String city;
private String country;
public Address() {
super();
}
public Address(String street, String city, String country) {
super();
this.street = street;
this.city = city;
this.country = country;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address [street=" + street + ", city=" + city + ", country="
+ country + "]";
}
}
Java代码
public class Person {
private String name;
private int age;
private boolean male;
private Address address;
public Person() {
super();
}
public Person(String name, int age, boolean male, Address address) {
super();
this.name = name;
this.age = age;
this.male = male;
this.address = address;
}
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 male;
}
public void setMale(boolean male) {
this.male = male;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", male=" + male
+ ", address=" + address + "]";
}
}
public class Person {
private String name;
private int age;
private boolean male;
private Address address;
public Person() {
super();
}
public Person(String name, int age, boolean male, Address address) {
super();
this.name = name;
this.age = age;
this.male = male;
this.address = address;
}
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 male;
}
public void setMale(boolean male) {
this.male = male;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", male=" + male
+ ", address=" + address + "]";
}
}下面是具体的解析代码:
Java代码
JSONObject jsonObject = new JSONObject(json);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
JSONObject addressJSON = jsonObject.getJSONObject("address");
String street = addressJSON.getString("street");
String city = addressJSON.getString("city");
String country = addressJSON.getString("country");
Address address = new Address(street, city, country);
Person person = new Person(name, age, male, address);
System.out.println(person);
JSONObject jsonObject = new JSONObject(json);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
JSONObject addressJSON = jsonObject.getJSONObject("address");
String street = addressJSON.getString("street");
String city = addressJSON.getString("city");
String country = addressJSON.getString("country");
Address address = new Address(street, city, country);
Person person = new Person(name, age, male, address);
System.out.println(person);LogCat的输出为Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]],说明已经正确解析了JSON对象.
2. 采用gson库解析JSON对象.假设带解析的JSON数据是json = "{\"name\":\"coolxing\", \"age\"=24, \"male\":true, \"address\":{\"street\":\"huiLongGuan\", \"city\":\"beijing\", \"country\":\"china\"}}",首先需要到http://code.google.com/p/google-gson/下载jar包,并将其添加到项目中.具体的解析代码如下:
Java代码
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);
System.out.println(person);
Gson gson = new Gson();
Person person = gson.fromJson(json, Person.class);
System.out.println(person);LogCat的输出为Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]],说明已经正确解析了JSON对象,是不是很简单?
3. 采用android内置的org.json包解析JSON数组.假设待解析的JSON数据为json = "[{\"name\":\"coolxing\", \"age\"=24, \"male\":true, \"address\":{\"street\":\"huiLongGuan\", \"city\":\"beijing\", \"country\":\"china\"}}, {\"name\":\"min\", \"age\"=20, \"male\":false, \"address\":{\"street\":\"heiShiJiao\", \"city\":\"daLian\", \"country\":\"china\"}}]",解析代码如下:
Java代码
List<Person> persons = new ArrayList<Person>();
JSONArray jsonArray = new JSONArray(json);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
JSONObject addressJSON = jsonObject.getJSONObject("address");
String street = addressJSON.getString("street");
String city = addressJSON.getString("city");
String country = addressJSON.getString("country");
Address address = new Address(street, city, country);
Person person = new Person(name, age, male, address);
persons.add(person);
}
System.out.println(persons);
List<Person> persons = new ArrayList<Person>();
JSONArray jsonArray = new JSONArray(json);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean male = jsonObject.getBoolean("male");
JSONObject addressJSON = jsonObject.getJSONObject("address");
String street = addressJSON.getString("street");
String city = addressJSON.getString("city");
String country = addressJSON.getString("country");
Address address = new Address(street, city, country);
Person person = new Person(name, age, male, address);
persons.add(person);
}
System.out.println(persons);LogCat的输出为[Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]], Person [name=min, age=20, male=false, address=Address [street=heiShiJiao, city=daLian, country=china]]],说明已经正确解析了JSON数组.
4. 采用gson库解析JSON数组.待解析的JSON数据同上,具体代码为:
Java代码
Gson gson = new Gson();
Type listType = new TypeToken<List<Person>>(){}.getType();
List<Person> persons = gson.fromJson(json, listType);
Gson gson = new Gson();
Type listType = new TypeToken<List<Person>>(){}.getType();
List<Person> persons = gson.fromJson(json, listType);LogCat的输出为[Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]], Person [name=min, age=20, male=false, address=Address [street=heiShiJiao, city=daLian, country=china]]],说明已经正确解析了JSON数组.
new TypeToken<List<Person>>(){}.getType();这句代码表示创建一个TypeToken的匿名子类对象,并调用对象的getType()方法.
org.json包和gson库还有很多其他有用的API,需要的时候可查看文档.比如有的时候需要通过java对象或java集合和数组生成json数据上传给服务器,当然你可以自我构造json字符串,但会很麻烦.这个时候就可以使用其中的有关API,方便的完成这项工作.