JSON数据转换


  1. 测试代码
String goodsPrice = "{"appid":"1637474419867659","code":0,"msg":"","qid":"c167447f-78da-4db4-b886-623fecc45335"," +
                ""result":true,"resultList":[{"region_code":"5010","barcode":"40"}],"sign":"22ACBF590295DC4D05017F69D382ADD6"," +
                ""timestamp":1620371185,"version":"2.3"}";

BaseResponse<StorePriceQueryResp> goods = JSON.parseObject(goodsPrice, new TypeReference<BaseResponse<StorePriceQueryResp>>() {
});

System.out.println(JSON.toJSON(goods));
//TODO: {"msg":"","result":true,"resultList":[{"barcode":"40","region_code":"5010"}]}

  1. 泛型类
@Data
public class BaseResponse<T> {

    private String msg;

    private Boolean result;

    private List<T> resultList;

    private Integer total;
}

  1. 实体类
@Data
public class StorePriceQueryResp {

    /**
     * ERP门店编码
     */
    private String region_code;

    /**
     * 商品条码
     */
    private String barcode;

    /**
     * ERP商品编码
     */
    private String item_code;

    /**
     * 牵牛花内码
     */
    private Long rid;

    /**
     * 当前售价
     */
    private String sale_price;

    /**
     * 市场价
     */
    private String reference_price;

    /**
     * 最近修改时间
     */
    private String timestamp;
}

  1. 原数据
{
    "appid": "1637474419867659",
    "code": 0,
    "msg": "",
    "qid": "c167447f-78da-4db4-b886-623fecc45335",
    "result": true,
    "resultList": [
        {
            "region_code": "5010",
            "barcode": "40"
        }
    ],
    "sign": "22ACBF590295DC4D05017F69D382ADD6",
    "timestamp": 1620371185,
    "version": "2.3"
}

  1. 转换后数据
{
    "msg": "",
    "result": true,
    "resultList": [
        {
            "barcode": "40",
            "region_code": "5010"
        }
    ]
}
原文地址:https://www.cnblogs.com/Twittery/p/14741364.html