JSON字符串,转为对象的操作失败以防日后踩坑。
- 错误信息
not match : - =, info : pos 10, line 1, column 11
错误的代码
//使用JSONObject接受前端的回传
//@RequestBody JSONObject jsonParam
@PostMapping("/inserts")
public R inserts(@RequestBody JSONObject jsonParam){
String data = jsonParam.getString("data");
//List<Drugregistration> drugregistrationList = (List<Drugregistration>) JSONArray.parseArray(data, Drugregistration.class);
List<Drugregistration> drugregistrationList =JsonUtils.fromListJson(data,Drugregistration.class );
boolean isok = drugregistrationService.saveBatch(drugregistrationList);
return R.ok(isok);
}
//改成String接收前端回传,然后再将字符串通过JSONObject.parseObject(str)转换成Json对象
//@RequestBody String str
@PostMapping("/inserts")
public R inserts(@RequestBody String str){
JSONObject jsonObject = JSONObject.parseObject(str);
String data = jsonObject.getString("data");
//List<Drugregistration> drugregistrationList = (List<Drugregistration>) JSONArray.parseArray(data, Drugregistration.class);
List<Drugregistration> drugregistrationList =JsonUtils.fromListJson(data,Drugregistration.class );
boolean isok = drugregistrationService.saveBatch(drugregistrationList);
return R.ok(isok);
}
主要的问题就出现在getString这句代码
错误代码中getString得到的值为
{id=66, firmware_id=15, version=V20190617_170943, file_hash=c9782aeff870c229f4a9d3efbaa11fa7, file_size=511020, description=测试, product_id=8}
而正确代码中得到的是
{"id":"66","firmware_id":"15","version":"V20190617_170943","file_hash":"c9782aeff870c229f4a9d3efbaa11fa7","file_size":"511020","description":"测试","product_id":"8"}
错误的代码中得到的值,没有双引号,是一个对象型的,parseObject转换需要是一个字符串因而报错
网友评论