参考:
超好用的json解析工具——Gson项目使用介绍
JAVA_Gson解析json工具类封装
Gson是通过toJson和fromJson两个方法进行转换的,使用起来十分方便。
Gson工具类:
package com.example.administrator.gsondemo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2017/5/9.
*/
public class GsonUtil {
private static Gson gson = null;
static {
if (gson == null) {
gson = new Gson();
}
}
private GsonUtil() {
}
/**
* 将object对象转成json字符串
*
* @param object
* @return
*/
public static String GsonString(Object object) {
String gsonString = null;
if (gson != null) {
gsonString = gson.toJson(object);
}
return gsonString;
}
/**
* 将gsonString转成泛型bean
*
* @param gsonString
* @param cls
* @return
*/
public static <T> T GsonToBean(String gsonString, Class<T> cls) {
T t = null;
if (gson != null) {
t = gson.fromJson(gsonString, cls);
}
return t;
}
/**
* 转成list
* 泛型在编译期类型被擦除导致报错
*
* @param gsonString
* @param cls
* @return
*/
public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
List<T> list = null;
if (gson != null) {
list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
}.getType());
}
return list;
}
/**
* 转成list
* 解决泛型问题
*
* @param json
* @param cls
* @param <T>
* @return
*/
public static <T> List<T> jsonToList(String json, Class<T> cls) {
Gson gson = new Gson();
List<T> list = new ArrayList<T>();
JsonArray array = new JsonParser().parse(json).getAsJsonArray();
for (final JsonElement elem : array) {
list.add(gson.fromJson(elem, cls));
}
return list;
}
/**
* 转成list中有map的
*
* @param gsonString
* @return
*/
public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
List<Map<String, T>> list = null;
if (gson != null) {
list = gson.fromJson(gsonString,
new TypeToken<List<Map<String, T>>>() {
}.getType());
}
return list;
}
/**
* 转成map的
*
* @param gsonString
* @return
*/
public static <T> Map<String, T> GsonToMaps(String gsonString) {
Map<String, T> map = null;
if (gson != null) {
map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
}.getType());
}
return map;
}
}
Person 类
package com.example.administrator.gsondemo;
/**
* Created by Administrator on 2017/5/9.
*/
public class Person {
private String name;
private int age;
private 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;
}
public Person() {
}
public Person(String name, int age, Birthday birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
Birthday 类
package com.example.administrator.gsondemo;
/**
* Created by Administrator on 2017/5/9.
*/
public class Birthday {
private int year;
private int month;
private int 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;
}
public Birthday() {
}
public Birthday(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return "Birthday{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
MainActivity 测试类:
package com.example.administrator.gsondemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
objectsToJson();
objectToJson();
}
/**
* 单一对象的json生成与解析
*/
public void objectToJson() {
Person p = new Person("zhaokaiqiang", 22, new Birthday(1992, 1, 19));
String jsonString = GsonUtil.GsonString(p);
Log.d(TAG, "---------单一对象生成字符串--------");
Log.d(TAG, jsonString);
Person person = GsonUtil.GsonToBean(jsonString, Person.class);
Log.d(TAG, "---------单一字符串解析成对象--------");
Log.d(TAG, person.toString());
}
/**
* 集合对象的json生成与解析
*/
public void objectsToJson() {
Person person1 = new Person("zhaokaiqiang1", 21, new Birthday(1991, 1, 19));
Person person2 = new Person("zhaokaiqiang2", 22, new Birthday(1992, 2, 29));
Person person3 = new Person("zhaokaiqiang3", 23, new Birthday(1993, 3, 39));
ArrayList<Person> arrayList = new ArrayList<Person>();
arrayList.add(person1);
arrayList.add(person2);
arrayList.add(person3);
String jsonString = GsonUtil.GsonString(arrayList);
Log.d(TAG, "---------集合对象生成字符串--------");
Log.d(TAG, jsonString);
List<Person> persons = GsonUtil.GsonToList(jsonString, Person.class);
Log.d(TAG, "---------集合字符串解析成对象--------");
Log.d(TAG, persons.toString());
}
}
总结:
在使用gson.toJson(object)方法的时候,我们可以把任何对象或者是集合放进去,就可以完美的转换成json格式。
如果想把json格式的数据转换成我们想要的对象,我们只需要调用gson.fromJson(gsonString, cls)即可。
但是在转换的时候,第二个参数有两种形式,
一种是Class< ? >,这个是转换单一的对象的时候,我们需要把对象.class传递进去,告诉转换器要转换的类型。
如果我们需要转换的是集合,我们需要传递一个实现Type接口的实例,具体语法就和上面的代码一样,集合类型还有具体的对象换一下就可以了。TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换。