代码如下:
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
/**
* 测试用户类
*
* @author:gan
* @date: 2023-04-28 15:33
*/
@Data
public class User {
private String name;
private Integer age;
private Date birthday;
private char sex;
private Float height;
private Double weight;
private BigDecimal money;
private Boolean adult;
//下面这些初始化为空,用于后面测试效果时用
private String nameDesc;
private Integer ageDesc;
private Date birthdayDesc;
private char sexDesc;
private Float heightDesc;
private Double weightDesc;
private BigDecimal moneyDesc;
private Boolean adultDesc;
public User() {
}
public User(String name, Integer age, Date birthday, char sex, Float height, Double weight, BigDecimal money, Boolean adult) {
this.name = name;
this.age = age;
this.birthday = birthday;
this.sex = sex;
this.height = height;
this.weight = weight;
this.money = money;
this.adult = adult;
}
}
初始化并输出一下:
public static void main(String[] args) {
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
}
注意,如果没有使用“@Data”注解,一定要加上get和set方法,不然转化之后的Json是空的!
主要是对SerializerFeature枚举类的使用,详细的可以看这个博客:
https://blog.csdn.net/xiang__liu/article/details/81570923
或者看这里:
https://www.javadoc.io/static/com.alibaba/fastjson/1.2.2/com/alibaba/fastjson/serializer/SerializerFeature.html#BeanToArray
下面是对SerializerFeature的使用演示:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
运行结果如下:
可以看到,直接使用toJSONString将Java实体转化为Json,会把没有显示初始化的字段丢掉。
“\u0000”是char类型的默认值,相对于null。
加上“SerializerFeature.WriteMapNullValue”,会输出值为空的字段。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
运行结果如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
运行结果如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
运行结果如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
运行结果如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
运行结果如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
String jsonStr8 = JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonStr8:" + jsonStr8);
运行结果如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
String jsonStr8 = JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonStr8:" + jsonStr8);
String jsonStr9 = JSON.toJSONString(user, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println("jsonStr9:" + jsonStr9);
运行结果如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSON.toJSONString start================================");
String jsonStr1 = JSON.toJSONString(user);
System.out.println("jsonStr1:" + jsonStr1);
String jsonStr2 = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonStr2:" + jsonStr2);
String jsonStr3 = JSON.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonStr3:" + jsonStr3);
String jsonStr4 = JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonStr4:" + jsonStr4);
String jsonStr5 = JSON.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonStr5:" + jsonStr5);
String jsonStr6 = JSON.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonStr6:" + jsonStr6);
String jsonStr7 = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonStr7:" + jsonStr7);
String jsonStr8 = JSON.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonStr8:" + jsonStr8);
String jsonStr9 = JSON.toJSONString(user, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println("jsonStr9:" + jsonStr9);
String jsonStr10 = JSON.toJSONString(user, SerializerFeature.PrettyFormat);
System.out.println("jsonStr10:" + jsonStr10);
运行结果如下:
JSONObject是JSON的子类,并且没有重写“toJSONString”方法,所以效果是一样的。
代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
System.out.println("=================================JSONObject.toJSONString start================================");
String jsonObjectStr1 = JSONObject.toJSONString(user);
System.out.println("jsonObjectStr1:" + jsonObjectStr1);
String jsonObjectStr2 = JSONObject.toJSONString(user, SerializerFeature.WriteMapNullValue);
System.out.println("jsonObjectStr2:" + jsonObjectStr2);
String jsonObjectStr3 = JSONObject.toJSONString(user, SerializerFeature.WriteNonStringValueAsString);
System.out.println("jsonObjectStr3:" + jsonObjectStr3);
String jsonObjectStr4 = JSONObject.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty);
System.out.println("jsonObjectStr4:" + jsonObjectStr4);
String jsonObjectStr5 = JSONObject.toJSONString(user, SerializerFeature.WriteNullListAsEmpty);
System.out.println("jsonObjectStr5:" + jsonObjectStr5);
String jsonObjectStr6 = JSONObject.toJSONString(user, SerializerFeature.QuoteFieldNames);
System.out.println("jsonObjectStr6:" + jsonObjectStr6);
String jsonObjectStr7 = JSONObject.toJSONString(user, SerializerFeature.WriteDateUseDateFormat);
System.out.println("jsonObjectStr7:" + jsonObjectStr7);
String jsonObjectStr8 = JSONObject.toJSONString(user, SerializerFeature.WriteNullNumberAsZero);
System.out.println("jsonObjectStr8:" + jsonObjectStr8);
String jsonObjectStr9 = JSONObject.toJSONString(user, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println("jsonObjectStr9:" + jsonObjectStr9);
String jsonObjectStr10 = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat);
System.out.println("jsonObjectStr10:" + jsonObjectStr10);
System.out.println("=================================JSONObject.toJSONString end================================");
代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
ObjectMapper objectMapper = new ObjectMapper();
try {
String objectMapperStr = objectMapper.writeValueAsString(user);
System.out.println("objectMapperStr:" + objectMapperStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
运行结果如下:
writeValueAsString方法会保留没有显示初始化的的字段,除了char类型初始化值为“\u0000”,其他都为“null”。
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new Gson();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
“Gson gson = new Gson();”只能简单地将Java实体转为json,要保留没有显示初始化的字段,需要用到“GsonBuilder”初始化“Gson”,代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new GsonBuilder()
.serializeNulls()
.create();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
可以看到没有显示初始化的字段都保留下来了,并且值都为“null”。
上面可以看到时间格式默认是国际化格式,想要转换为“yyyy年MM月dd日 时:分:秒”怎么办?可以使用“setDateFormat”格式化时间,代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy年MM月dd日 HH:mm:ss")
.create();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
如果觉得转换之后的json只有一行不好看,可以使用“setPrettyPrinting”格式化json的格式,代码如下:
User user = new User("王宇", 25, DateUtil.parse("1998-10-12 13:25:22"), '男', 180.0f, 71.55, new BigDecimal("2104.5"), true);
System.out.println("toString方法:" + user);
Gson gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy年MM月dd日 HH:mm:ss")
.setPrettyPrinting()
.create();
String gsonStr1 = gson.toJson(user);
System.out.println("gsonStr1:" + gsonStr1);
运行结果如下:
拿到测试用的Java实体转化为Json,格式化后如下:
{
"adult": true,
"adultDesc": null,
"age": 25,
"ageDesc": null,
"birthday": 908169922000,
"birthdayDesc": null,
"height": 180.0,
"heightDesc": null,
"money": 2104.5,
"moneyDesc": null,
"name": "王宇",
"nameDesc": null,
"sex": "男",
"sexDesc": "\u0000",
"weight": 71.55,
"weightDesc": null
}
使用JSON的parseObject(String text, Class clazz)方法可以直接将Json字符转化为对应clazz的Java实体,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
User user = JSON.parseObject(userJson, User.class);
System.out.println("user:" + user);
运行结果如下:
JSONObject中转化Java实体的方法parseObject也是来自JSON,并且没有重写,效果是一样的,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
User user = JSONObject.parseObject(userJson, User.class);
System.out.println("user:" + user);
运行结果如下:
需要先实例化ObjectMapper,再调用readValue方法,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
ObjectMapper objectMapper = new ObjectMapper();
try {
User user = objectMapper.readValue(userJson, User.class);
System.out.println("user:" + user);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
运行结果如下:
Gson实例化后,使用fromJson方法将Json转化为Java实体,代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
Gson gson = new Gson();
User user = gson.fromJson(userJson, User.class);
System.out.println("user:" + user);
需要使用GsonBuilder进行实例化,在适配一下Date类型的字段。代码如下:
String userJson = "{\"adult\":true,\"adultDesc\":null,\"age\":25,\"ageDesc\":null,\"birthday\":908169922000,\"birthdayDesc\":null,\"height\":180.0,\"heightDesc\":null,\"money\":2104.5,\"moneyDesc\":null,\"name\":\"王宇\",\"nameDesc\":null,\"sex\":\"男\",\"sexDesc\":\"\\u0000\",\"weight\":71.55,\"weightDesc\":null}";
System.out.println("userJson:" + userJson);
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
})
.create();
User user = gson.fromJson(userJson, User.class);
System.out.println("user:" + user);
再次运行: