Java将json字符串转list对象(亲测)

Controller 用@RequestBody 和String 来接受数据

 public ResultJson validateInventury(@RequestBody String jsonObject)
参数

{"inventoryDTOs":[{
    "type": 0,
    "saleQuantity": 188.0000,
    "basePrice":1000.00,
    "salePrice": 3000.00,
    "inventoryId": 35
}, {
    "type": 0,
    "saleQuantity": 949.0000,
    "basePrice":200.00,
    "salePrice": 300.00,
    "inventoryId": 28
},{
    "type": 1,
    "saleQuantity": 94.0000,
    "basePrice":200.00,
    "salePrice": 1000.00,
    "inventoryId": 326    
}
]}

    转换:

    JSONObject jsonObject = JSONObject.parseObject(jsonObject);
         
        String versionInfoStr = jsonObject.getString("inventoryDTOs");
          
        List<InventoryDTO> inventoryDTOs = JSON.parseArray(versionInfoStr, InventoryDTO.class);
特别注意,当时以为外面可以用JSONObject接收,省掉了转换第一步,结果在转换的时候回报错。
————————————————
版权声明:本文为CSDN博主「All In丶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/superPojo/article/details/108793355