Json格式化簡單使用案例

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章