在实际项目中,我们往往不会用java原生的json解析类去解析json数据(代码太繁琐).Google极力推荐我们使用Google自己开源的json解析工具Gson,Gson解析工具是目前在项目中最流行的解析json的工具,那么我们一起来跟着大神的脚步,学习一下Gson的用法。以下方法基本都来自http://blog.csdn.net/zhaokaiqiang1992。
首先我们先要给出Gson2.3.1jar文件下载地址.
为了增加json解析的难度,我们先构建下面几个实体类,具体关系,看代码就会明白。
第一个是Birthday实体类:
package com.wj.gson.bean;
public class Birthday {
private int year;
private int month;
private int day;
public Birthday(){
}
public Birthday(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "Birthday [year=" + year + ", month=" + month + ", day=" + day
+ "]";
}
}
第二个是Person实体类
package com.wj.gson.bean;
public class Person {
private String name;
private int age;
private Birthday birthday;
public Person(){
}
public Person(String name, int age, Birthday birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}
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 Birthday getBirthday() {
return birthday;
}
public void setBirthday(Birthday birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", birthday="
+ birthday + "]";
}
}
1.Object数据与json数据相互转换.
public void objectToJson(){
Person p=new Person("zhangsan", 18, new Birthday(2014, 12, 12));
Gson gson=new Gson();
String jsonString=gson.toJson(p);
Log.e("wj", jsonString);
Person p1=gson.fromJson(jsonString, Person.class);
Log.e("wj", p1.toString());
}
很简单,如果我们用原始的jsonObject解析会很复杂,下面看一下运行结果
<span style="color:#ff0000;">12-11 01:57:05.667: E/wj(802): {"name":"zhangsan","birthday":{"day":12,"month":12,"year":2014},"age":18}
12-11 01:57:05.667: E/wj(802): Person [name=zhangsan, age=18, birthday=Birthday [year=2014, month=12, day=12]]
12-11 01:57:05.767: D/gralloc_goldfish(802): Emulator without GPU emulation detected.</span>
2.
集合泛型与Json的相互转换
Gson解析封装实体类的list:
public void listToJson(){
Gson gson=new Gson();
Person person=new Person("欧弟", 2, new Birthday(2011,1,1));
List<Person> personList=new ArrayList<Person>();
personList.add(person);
personList.add(person);
personList.add(person);
String jsonString=gson.toJson(personList);
Log.e("wj", "--------------集合生成Json----------------");
Log.e("wj", "jsonString:"+jsonString);
<span style="color:#ff0000;">Type listType=new TypeToken<List<Person>>(){}.getType();</span>
List<Person> persons=gson.fromJson(jsonString, listType);
Log.e("wj", "-------------Json解析成集合---------------");
Log.e("wj", persons.toString());
}
这里注意Type是这个import java.lang.reflect.Type;包里的。
解析结果:
<span style="color:#ff0000;">12-11 03:03:14.156: E/wj(943): --------------集合生成Json----------------
12-11 03:03:14.156: E/wj(943): jsonString:[{"name":"欧弟","birthday":{"day":1,"month":1,"year":2011},"age":2},{"name":"欧弟","birthday":{"day":1,"month":1,"year":2011},"age":2},{"name":"欧弟","birthday":{"day":1,"month":1,"year":2011},"age":2}]
12-11 03:03:14.166: E/wj(943): -------------Json解析成集合---------------
12-11 03:03:14.166: E/wj(943): [Person [name=欧弟, age=2, birthday=Birthday [year=2011, month=1, day=1]], Person [name=欧弟, age=2, birthday=Birthday [year=2011, month=1, day=1]], Person [name=欧弟, age=2, birthday=Birthday [year=2011, month=1, day=1]]]</span>
比如这样一个json数据,{"code":200,"msg":"ok","person":{"name":"欧弟","birthday":{"day":12,"month":6,"year":1990},"age":14}}
我们来看类似这样的数据我们如何生成,先来了解几个Gson解析中非常重要的对象:
Gson:这个类在前面已经用过了,是我们完成json转换和解析的主要类,主要使用toJson()和fromJson()这两个方法。
JsonObject:这个是我们构建Object形式的Json数据的主要类,我们可以设置key和value。
JsonElement:这个是Json元素类,它里面可以是一个JsonObject、JsonArray、JsonNull或者是一个JsonPrimitive。注意,它里面的数据只能看作是一个元素。
JsonArray:这个是我们想构造Array形式的主要类,我们可以往数组里面添加数据,数据是有序的。
JsonParser:这个是Json的解析器,主要作用是把一个Json形式的字符串转换成一个JsonElement对象。
ok,看我们用代码如何使用这些对象: public String varToJson(){
Person p=new Person("欧弟", 14, new Birthday(1990, 6, 12));
JsonElement jsonElement=new JsonParser().parse(new Gson().toJson(p));
JsonObject jsonObject=new JsonObject();
jsonObject.addProperty("code", 200);
jsonObject.addProperty("msg", "ok");
jsonObject.add("person", jsonElement);
Log.e("wj", "生成负责Json数据"+jsonObject.toString());
return jsonObject.toString();
}
在上面的方法中,我们使用JsonParser和Gson只是为了获取一个JsonElement对象,因为使用JsonObject构造Object形式的Json数据的时候,基本数据类型可以使用addProperty()方法直接添加进去,但是如果想添加类的话,必须包装成一个JsonElement对象之后才能够添加,所以先用Gson的toJson()转换成一个Json格式的数据,然后使用JsonParser转换成一个JsonElement,使用add()添加进去之后,直接JsonObject.toString()就可以获取到复杂对象的Json格式数据了。
下面是输出结果:
<span style="color:#ff0000;">12-11 03:19:16.346: E/wj(989): 生成负责Json数据{"code":200,"msg":"ok","person":{"name":"欧弟","birthday":{"day":12,"month":6,"year":1990},"age":14}}</span>
最后我们再来解析上面这条数据:
public void fromJsonVar(String str){
Gson gson=new Gson();
JsonElement jsonElement=new JsonParser().parse(str);
JsonObject jsonObject=jsonElement.getAsJsonObject();
JsonElement personElement=jsonObject.get("person");
Person p=gson.fromJson(personElement, Person.class);
JsonElement msgElement=jsonObject.get("msg");
String name=msgElement.getAsString();
JsonElement codeElement=jsonObject.get("code");
int code=codeElement.getAsInt();
Log.e("wj", "person-----"+p);
Log.e("wj", "name-----"+name);
Log.e("wj", "code-----"+code);
}
看一下结果:
<span style="color:#ff0000;"><span style="color:#ff0000;">12-11 03:38:01.166: E/wj(1037): person-----Person [name=欧弟, age=14, birthday=Birthday [year=1990, month=6, day=12]]
12-11 03:38:01.166: E/wj(1037): name-----ok
12-11 03:38:01.166: E/wj(1037): code-----200</span></span>
ok,用法基本都了解了。剩下的就是在实战中操练了..................