fastjson
簡單解析user_name屬性
String user = ((Map) JSON.parse(token)).get("user_name").toString();
解析自定義對象,兩種方法都可以
Student stu1=JSON.parseObject(jsonstr,new TypeReference<Student>(){});
Student stu1=JSON.parseObject(jsonstr,Student.class);
Gson
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
items: [{
productId: "1423113435324",
productQuantity: 2
}]
List<OrderDetail> orderDetailList = new ArrayList<>();
try{
orderDetailList = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>() {
}.getType());
} catch (Exception e) {
log.error("【對象轉換】錯誤,string={}", orderForm.getItems());
throw new SellException(ResultEnum.PARAM_ERROR);
}
參考資料
https://blog.csdn.net/srj1095530512/article/details/82529759
https://www.jianshu.com/p/f20ffefeec4d