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); //json字符串转换成jsonobject对象。
String versionInfoStr = jsonObject.getString(“inventoryDTOs”);//通过key获取值
List inventoryDTOs = JSON.parseArray(versionInfoStr, InventoryDTO.class);//将json字符串转换成list
特别注意,当时以为外面可以用JSONObject接收,省掉了转换第一步,结果在转换的时候回报错。
拓展:string转换成json字符串
String json_str = JSON.toJSONString(string字符串);
本文介绍了在Controller中如何使用@RequestBody和String注解接收JSON数据,详细展示了转换过程,包括从JSON字符串到JSONObject,再到List<InventoryDTO>对象的转换步骤。同时,指出了在尝试直接用JSONObject接收时遇到的错误,提醒开发者注意正确的数据转换方法。
8074

被折叠的 条评论
为什么被折叠?



