需要导入:gson-2.2.4.jar
一、JSON与数组互转:
//将数组转JSON
int[] number = {1,2,3,4,5};
Gson gson = new Gson();
String numberjson = gson.toJson(number);
//将JSON转数组
int[] numberNew = gson.fromJson(numberjson, int[].class);
二、 JSON与JavaBean互转:
// JavaBean转JSON
Student stu = new Student("1001", "lwc");
Gson gson = new Gson();
String jsondata = gson.toJson(stu);
// JSON转JavaBean
stu = gson.fromJson(jsondata, Student.class);
三、 JSON与List互转:
// List转JSON
List list = new ArrayList();
for (int i = 0; i < 5; i++) {
list.add("element" + i);
}
Gson gson = new Gson();
String json = gson.toJson(list);
// JSON转List
List list2 = (List) gson.fromJson(json, Object.class);
四、 JSON与泛型List互转:
// 泛型Lis转JSON
List<Student> list = new ArrayList<Student>();
for (int i = 0; i < 3; i++) {
Student stu = new Student("100" + i, "name" + i);
list.add(stu);
}
Type type = newTypeToken<List<Student>>() {}.getType();
Gson gson = new Gson();
String json = gson.toJson(list, type);
//JSON转泛型Lis
List<Student> users = gson.fromJson(json.toString(), type);
五、Map转json:
// Map转JSON
Map map = new HashMap();
map.put("no", "1001");
map.put("name", "lwc");
Gson gson = new Gson();
String json = gson.toJson(map);
六、JSON与泛型Map互转:
// JSON转泛型Map
Map<String, Student> map = new HashMap<String, Student>();
for (int i = 0; i < 2; i++) {
Student stu = new Student("100" + i, "name" + i);
map.put("100" + i, stu);
}
Type type = new TypeToken<Map<String, Student>>() {}.getType();
Gson gson = new Gson();
String json = gson.toJson(map, type);
// 转泛型Map转JSON
Map<String, Student> mapStu = gson.fromJson(json.toString(), type);
七、 JSON与带日期JavaBean互转:
/**
* 日期序列化实用工具类
*/
class DateDeserializerUtils implements JsonDeserializer<java.util.Date> {
public Date deserialize(JsonElement arg0, Type arg1,
JsonDeserializationContext arg2) throws JsonParseException {
return new java.util.Date(arg0.getAsJsonPrimitive().getAsLong());
}
}
/**
* 日期解序列实用工具类
*/
class DateSerializerUtils implements JsonSerializer<java.util.Date> {
public JsonElement serialize(Date arg0, Type arg1,
JsonSerializationContext arg2) {
return new JsonPrimitive(arg0.getTime());
}
}
public class Test {
public static void main(String[] args) {
// 带日期JavaBean转JSON
Student stu = new Student("1001", "lwc", new Date());
Gson gson = new Gson();
gson = new GsonBuilder().registerTypeAdapter(Date.class,
new DateSerializerUtils()).setDateFormat(DateFormat.LONG).create();
String json = gson.toJson(stu);
// JSON转带日期JavaBean
gson = new GsonBuilder().registerTypeAdapter(Date.class,
new DateDeserializerUtils()).setDateFormat(DateFormat.LONG).create();
Type type = new TypeToken<Student>() {}.getType();
Student s = gson.fromJson(json, type);
// 泛型日期List转JSON
List<Student> list = new ArrayList<Student>();
list.add(new Student("1001", "name1", new Date()));
list.add(new Student("1002", "name2", new Date()));
gson = new GsonBuilder().registerTypeAdapter(Date.class,
new DateSerializerUtils()).setDateFormat(DateFormat.LONG).create();
Type type2 = new TypeToken<List<Student>>() {}.getType();
json = gson.toJson(list, type2);
// JSON转泛型日期List
gson = new GsonBuilder().registerTypeAdapter(Date.class,
new DateDeserializerUtils()).setDateFormat(DateFormat.LONG).create();
List<Student> stus = gson.fromJson(json.toString(), type);
}
}