list集合中integer问题
List<Integer> lists= gson.fromJson(params.get("lists"), new TypeToken<ArrayList<Integer>>() {}.getType());
空值不转换问题
Gson gson = new GsonBuilder().serializeNulls().create();
日期格式化
Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();
object转string
public static String parseJsonSetId(Object obj) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(ObjectId.class,
new JsonSerializer<ObjectId>() {
@Override
public JsonElement serialize(ObjectId arg0, Type arg1,
JsonSerializationContext arg2) {
return new JsonPrimitive(arg0.toString());
}
});
Gson gson = builder.create();
return gson.toJson(obj);
}
integer默认转double解决办法 下面方法貌似不能用的
Gson gson = new GsonBuilder().registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
@Override
public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
if (src == src.longValue())
return new JsonPrimitive(src.longValue());
return new JsonPrimitive(src);
}
}).create();