json之com.google.gson.Gson

本文介绍如何使用Gson库实现JSON与Java对象之间的转换,包括基本数据类型、数组、JavaBean、List集合、Map集合及带有日期类型的JavaBean等。通过示例代码详细展示了不同场景下数据转换的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要导入: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);
    }
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值