Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 4171 异常的解决方法

在做本地json文件的解析时遇到了这个问题

原代码为

@RequestMapping(value= "readJson1")
	public String readJson1(String cityJsonCode){
		// json解析器
		JsonParser parser = new JsonParser();
		try {
			String root = System.getProperty("user.dir");
			String abcPath = root + File.separator + "file" + File.separator + "130300.json";
			FileReader fReader = new FileReader(abcPath);;
			JsonObject object=(JsonObject) parser.parse(fReader);;
			// json数组
			JsonArray array=object.get("arry").getAsJsonArray(); 
			for (int i = 0; i < array.size(); i++) {
				CountyEntity countyEntity = new CountyEntity();
				JsonObject subObject=array.get(i).getAsJsonObject();
				// 编码
				String code = subObject.get("代码").getAsString();
				// 县级名称
				String name = subObject.get("县级名称").getAsString();
				// 经度
				String jingdu = subObject.get("经度").getAsString();
				// 纬度
				String weidu = subObject.get("纬度").getAsString();
				// 省级名称
				String provinceName = subObject.get("省级名称").getAsString();
				String provinceCode = provinceService.findProJsonCodefromProName(provinceName);
				if (!StringUtils.isBlank(provinceCode)) {
					countyEntity.setProvinceCode(provinceCode);
				}else{
					continue;
				}
				// 地级市名称
				String cityName = subObject.get("地级市名称").getAsString();
				String cityCode = cityService.findCityJsonByCityName(cityName);
				if (!StringUtils.isBlank(cityCode)) {
					countyEntity.setCityCode(cityCode);
				}else {
					continue;
				}
				// 设置插入值
				countyEntity.setId(IdGen.uuid());
				countyEntity.setCountyCode(code);
				countyEntity.setCountyName(name);
				countyEntity.setLongitude(jingdu);
				countyEntity.setLatitude(weidu);
				countyEntity.setCreateBy(countyEntity.getCurrentUser());
				countyEntity.setCreateDate(new Date());
				countyEntity.setUpdateBy(countyEntity.getCurrentUser());
				countyEntity.setUpdateDate(new Date());
				countyDao.insert(countyEntity);
			}
		} catch (JsonIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return "modules/sys/countyList";
	}

由于读取出的json格式错误导致parse的时候报异常

修改为如下代码问题解决

@RequestMapping(value= "readJson")
	public String readJson(String cityJsonCode){
		// json解析器
		JsonParser parser = new JsonParser();
		String jsonStr = "";
		// json对象
		try {
			String root = System.getProperty("user.dir");
			String abcPath = root + File.separator + "file" + File.separator + "130300.json";
			File file = new File(abcPath);
			Scanner scanner = null;
			StringBuilder buffer = new StringBuilder();
			try {
				scanner = new Scanner(file, "utf-8");
				while (scanner.hasNextLine()) {
					buffer.append(scanner.nextLine().trim());
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (scanner != null) {
					scanner.close();
				}
			}
			jsonStr = buffer.toString();
			JsonElement je = parser.parse(jsonStr);
			JsonObject object=(JsonObject) je;
			// json数组
			JsonArray array=object.get("array").getAsJsonArray(); 
			for (int i = 0; i < array.size(); i++) {
				CountyEntity countyEntity = new CountyEntity();
				JsonObject subObject=array.get(i).getAsJsonObject();
				// 编码
				String code = subObject.get("代码").getAsString();
				// 县级名称
				String name = subObject.get("县级名称").getAsString();
				// 经度
				String jingdu = subObject.get("经度").getAsString();
				// 纬度
				String weidu = subObject.get("纬度").getAsString();
				// 省级名称
				String provinceName = subObject.get("省级名称").getAsString();
				String provinceCode = provinceService.findProJsonCodefromProName(provinceName);
				if (!StringUtils.isBlank(provinceCode)) {
					countyEntity.setProvinceCode(provinceCode);
				}else{
					continue;
				}
				// 地级市名称
				String cityName = subObject.get("地级市名称").getAsString();
				String cityCode = cityService.findCityJsonByCityName(cityName);
				if (!StringUtils.isBlank(cityCode)) {
					countyEntity.setCityCode(cityCode);
				}else {
					continue;
				}
				// 设置插入值
				countyEntity.setId(IdGen.uuid());
				countyEntity.setCountyCode(code);
				countyEntity.setCountyName(name);
				countyEntity.setLongitude(jingdu);
				countyEntity.setLatitude(weidu);
				countyEntity.setCreateBy(countyEntity.getCurrentUser());
				countyEntity.setCreateDate(new Date());
				countyEntity.setUpdateBy(countyEntity.getCurrentUser());
				countyEntity.setUpdateDate(new Date());
				countyDao.insert(countyEntity);
			}
		} catch (JsonIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return "modules/sys/countyList";
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值