在做本地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";
}