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.gsongroupId>
    <artifactId>gsonartifactId>
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

你可能感兴趣的:(JAVA,日常总结)