先上例子:
private static Gson gson = new GsonBuilder().serializeNulls().create(); public static void main(String[] args) { Test test = new Test(); test.test(); } public void test() { List<DepartGroup> groups = new ArrayList<DepartGroup>(); DepartGroup runningGroup = new DepartGroup(); runningGroup.eTime = "00:00"; runningGroup.fTime = "40:00"; groups.add(runningGroup); String jsonResult = gson.toJson(groups); System.out.println(jsonResult); jsonResult = JSON.toJSONString(runningGroup); System.out.println(jsonResult); } class DepartGroup { private GroupTag tag; private String fTime; private String eTime; }
输出时这样的:
[{"tag":null,"fTime":"40:00","eTime":"00:00"}]
{}
gson在序列化时,不管属性是否私有,都可以序列化成功,
但是fastjson则不行,需要添加get方法或者改为public才行