fastjson 错误解决方案详情 com.alibaba.fastjson.JSONException: syntax error, expect {, actual EOF, pos 1410
原因:
前端传递的数组过于复杂,倒是出现这种问题,前端采用vue axios,发送请求,后端java接收代码,实现前后端分离
后端就收fastjson接收json,进行业务处理,后端Controller状况:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 3 * <p> 4 * <p>添加订单 5 * 6 * @return Object 7 */ 8 @ResponseBody 9 @RequestMapping(value = "/addOrder", // 10 method = RequestMethod.POST) 11 public Object addOrder(@RequestBody BaseSingleList baseSingleList) { 12 13 return orderService.addOrder(baseSingleList); 14 15 }
前端发送json样式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 { 2 "singleOrderList":[ 3 { 4 "orderName":"唐1", 5 "orderPhone":"13245124512", 6 "contact":"送", 7 "telephony":"7845120231111", 8 "provinceId":31, 9 "cityId":3101, 10 "regionId":310108, 11 "address":"默认添加唐", 12 "userId":"c6f53705451b497580ef093c0ff5", 13 "serieId":"1", 14 "trueTime":"2018-04-27", 15 "overTime":"2019-04-27", 16 "monthlyRent":6000, 17 "dateCount":12, 18 "packageId":"3e449fb4b4a489fce1475c4577fb6", 19 "applicationArea":"ssswww", 20 "total":"219000", 21 "ModularIdNum":[ 22 { 23 "itemId":"9b744dc99e2904d96ab1af5", 24 "modularNum":3 25 } 26 ] 27 }, 28 { 29 "orderName":"唐1", 30 "orderPhone":"13245124512", 31 "contact":"送", 32 "telephony":"7845120231111", 33 "provinceId":31, 34 "cityId":3101, 35 "regionId":310108, 36 "address":"默认添加唐", 37 "userId":"b4f13b97580ef093c0ff5", 38 "serieId":"1", 39 "trueTime":"2018-04-27", 40 "overTime":"2019-04-27", 41 "monthlyRent":6000, 42 "dateCount":12, 43 "packageId":"3b4b4a489fce1475c4577fb6", 44 "applicationArea":"ssswww", 45 "total":"219000", 46 "ModularIdNum":[ 47 { 48 "itemId":"09932da9b744dc99e295", 49 "modularNum":3 50 } 51 ] 52 } 53 ] 54 }
解决方案:
controller正常传入数据,在serviceImpl层中对数据进行分解和重新发送业务逻辑:
(1)当数据传入是JsonObject,用如下方法传递
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 下订单 3 * 4 * @param baseSingleList 5 * @return Object 6 */ 7 @Override 8 public Object addOrder(BaseSingleList baseSingleList) { 9 10 String lineArray = JSONArray.toJSONString(baseSingleList); 11 HashMap parseMap = JSON.parseObject(lineArray,HashMap.class); 12 13 List<SingleOrder> singleOrderList = JSON.parseArray(JSON.parseObject(lineArray).getString("singleOrderList"),SingleOrder.class); 14 15 for(SingleOrder singleOrder : singleOrderList){ 16 17 System.out.println(singleOrder.getUserId()); 18 } 19 }
(2)当数据传入是Map时
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 下订单 3 * 4 * @param baseSingleList 5 * @return Object 6 */ 7 @Override 8 public Object addOrder(BaseSingleList baseSingleList) { 9 10 11 12 Map<String, Object> map = new HashMap<String, Object>(); 13 map.put("count", 2); 14 map.put("studentList", baseSingleList); 15 String json = JSON.toJSONString(map, true); 16 17 18 HashMap parseMap = JSON.parseObject(json, HashMap.class); 19 List<BaseSingleList> studentList1 = (List<BaseSingleList>) parseMap.get("baseSingleList"); 20 21 for (SingleOrder singleOrder : singleOrderList) { 22 23 System.out.println(singleOrder.getUserId()); 24 } 25 26 }
参考文献 :
https://blog.csdn.net/jeffleo/article/details/73612224
十年磨一剑,一剑破万法