Gson是Google的一个开源项目,可以将Java对象转换成JSON,也可能将JSON转换成Java对象。
Gson里最重要的对象有2个Gson 和 GsonBuilder
Gson有2个最基本的方法
1) toJson() � 转换java 对象到JSON
2) fromJson() � 转换JSON到java对象
对于泛型对象,使用fromJson(String, Type)方法来将Json对象转换成对应的泛型对象
new TypeToken<>(){}.getType()方法取得Type
如: new TypeToken<List<String>>(){}.getType()
new TypeToken<List<Person>>(){}.getType()
new TypeToken<List<Map<String, String>>>(){}.getType()
GsonBuilder
//注意这里的Gson的构建方式为GsonBuilder,区别于Gson gson = new Gson();
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation() //不导出实体中没有用@Expose注解的属性
.enableComplexMapKeySerialization() //支持Map的key为复杂对象的形式
.serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//时间转化为特定格式
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.
.setPrettyPrinting() //对json结果格式化.
.setVersion(1.0) //有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.
//@Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么
//@Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.
.create();
参考阅读
Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 http://blog.csdn.net/lk_blog/article/details/7685169
Json转换利器Gson之实例二-Gson注解和GsonBuilder http://blog.csdn.net/lk_blog/article/details/7685190
Json转换利器Gson之实例三-Map处理(上) http://blog.csdn.net/lk_blog/article/details/7685210
Json转换利器Gson之实例四-Map处理(下) http://blog.csdn.net/lk_blog/article/details/7685224
Json转换利器Gson之实例五-实际开发中的特殊需求处理 http://blog.csdn.net/lk_blog/article/details/7685237 重要
Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型 http://blog.csdn.net/lk_blog/article/details/7685347 不常用
代码
辅助类
public class JsonService
{
public Person getPerson()
{
Person person = new Person(1, "xiaoluo", "广州");
return person;
}
public List<Person> getPersons()
{
List<Person> persons = new ArrayList<Person>();
Person person = new Person(1, "xiaoluo", "广州");
Person person2 = new Person(2, "android", "上海");
persons.add(person);
persons.add(person2);
return persons;
}
public List<String> getString()
{
List<String> list = new ArrayList<String>();
list.add("广州");
list.add("上海");
list.add("北京");
return list;
}
public List<Map<String, String>> getMapList()
{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("id", "001");
map1.put("name", "xiaoluo");
map1.put("age", "20");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("id", "002");
map2.put("name", "android");
map2.put("age", "33");
list.add(map1);
list.add(map2);
return list;
}
}
测试类
public static void main(String[] args)
{
Gson gson = new Gson();
JsonService jsonService = new JsonService();
Person person = jsonService.getPerson();
System.out.println("person: " + gson.toJson(person));
// 对于Object类型,使用 fromJson(String, Class)方法来将Json对象转换成Java对象
Person person2 = gson.fromJson(gson.toJson(person), Person.class);
System.out.println(person2);
System.out.println("------------------------------------------------");
List<Person> persons = jsonService.getPersons();
System.out.println("persons: " + gson.toJson(persons));
/*
* 对于泛型对象,使用fromJson(String, Type)方法来将Json对象转换成对应的泛型对象
* new TypeToken<>(){}.getType()方法
*/
List<Person> persons2 = gson.fromJson(gson.toJson(persons), new TypeToken<List<Person>>(){}.getType());
System.out.println(persons2);
System.out.println("------------------------------------------------");
List<String> list = jsonService.getString();
System.out.println("String---->" + gson.toJson(list));
List<String> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<String>>(){}.getType());
System.out.println("list2---->" + list2);
System.out.println("------------------------------------------------");
List<Map<String, String>> listMap = jsonService.getMapList();
System.out.println("Map---->" + gson.toJson(listMap));
List<Map<String, String>> listMap2 = gson.fromJson(gson.toJson(listMap), new TypeToken<List<Map<String, String>>>(){}.getType());
System.out.println("listMap2---->" + listMap2);
System.out.println("------------------------------------------------");
}
输出结果
person: {"id":1,"name":"xiaoluo","address":"广州"}
Person [id=1, name=xiaoluo, address=广州]
------------------------------------------------
persons: [{"id":1,"name":"xiaoluo","address":"广州"},{"id":2,"name":"android","address":"上海"}]
[Person [id=1, name=xiaoluo, address=广州], Person [id=2, name=android, address=上海]]
------------------------------------------------
String---->["广州","上海","北京"]
list2---->[广州, 上海, 北京]
------------------------------------------------
Map---->[{"id":"001","age":"20","name":"xiaoluo"},{"id":"002","age":"33","name":"android"}]
listMap2---->[{id=001, age=20, name=xiaoluo}, {id=002, age=33, name=android}]
------------------------------------------------