gson对空值属性自动去除

使用Gson的toJson方法时,会自动去掉值为null的键值对。

        String sql = "select * from user left join uexam on uexam.uno=user.uno";
        System.out.println(sql);
        List<Map<String, Object>> maps = ViewJDBC.queryMapList(sql);
        for (Map<String, Object> map : maps) {
            System.out.println(map);
        }
        System.out.println(Message.toJson(maps));

在sql使用join时,可能导致部分行的部分列为空,返回的原数据是正常的。

而使用 Gson 转 json 字符串后,一些为 null 的键就不存在了,对前端来说还是不错的,毕竟 null 和 undefined 都可以使用 ! 自动转boolean判断。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注