加载中......

JSON字符串,转为对象操作失败not match : - =, info : pos 10, line 1, column 11

  • 报错信息

    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转换需要是一个字符串因而报错

作者:Time一柒
链接:https://www.jianshu.com/p/07b1818f040d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

posted @ 2023-01-31 12:05  MarchXD  阅读(495)  评论(0编辑  收藏  举报