原json文件里面的内容是json数组, 是国家数据,现在想给每个国家做一个大洲归类, 故想给数组里面的json对象新增一个记录大洲的属性,通过手动增加了国家对应的大洲之后,将原本的json数组改成 根据大洲归类好的json对象
先解析json文件成一个JSONArray对象,然后遍历出每一个json对象 新增6大洲的json数组 通过比对大洲的属性 归类好再将数组添加至map
@RequestMapping(value = "/countriesList2",method = RequestMethod.GET)
@ResponseBody
public Map<String,JSONArray> countriesList2() throws IOException {
HashMap<String, JSONArray> map = new HashMap<>();
String path = "/countries_list.json";
JSONArray json = null;
InputStream config = getClass().getResourceAsStream(path);
if (config == null) {
new BaseException("读取文件失败");
} else {
json = JSON.parseObject(config, JSONArray.class);
JSONArray asiaArray = new JSONArray();
JSONArray europeArray = new JSONArray();
JSONArray africaArray = new JSONArray();
JSONArray southAArray = new JSONArray();
JSONArray northAArray = new JSONArray();
JSONArray oceaniaArray = new JSONArray();
for (int i = 0; i < json.size(); i++) {
JSONObject jsonObject = new JSONObject();
JSONObject key1 = (JSONObject)json.get(i);
String abb2 = (String)key1.get("abb2");
String abb3 = (String)key1.get("abb3");
String cn = (String)key1.get("cn");
String code = (String)key1.get("code");
String en = (String)key1.get("en");
String full = (String)key1.get("full");
String zhou = (String)key1.get("zhou");
if(StringUtils.equals("Asia",zhou)){
jsonObject.put("abb2",abb2);
jsonObject.put("abb3",abb3);
jsonObject.put("cn",cn);
jsonObject.put("code",code);
jsonObject.put("en",en);
jsonObject.put("full",full);
jsonObject.put("zhou",zhou);
asiaArray.add(jsonObject);
}
if(StringUtils.equals("Europe",zhou)){
jsonObject.put("abb2",abb2);
jsonObject.put("abb3",abb3);
jsonObject.put("cn",cn);
jsonObject.put("code",code);
jsonObject.put("en",en);
jsonObject.put("full",full);
jsonObject.put("zhou",zhou);
europeArray.add(jsonObject);
}
if(StringUtils.equals("Africa",zhou)){
jsonObject.put("abb2",abb2);
jsonObject.put("abb3",abb3);
jsonObject.put("cn",cn);
jsonObject.put("code",code);
jsonObject.put("en",en);
jsonObject.put("full",full);
jsonObject.put("zhou",zhou);
africaArray.add(jsonObject);
}
if(StringUtils.equals("SouthA",zhou)){
jsonObject.put("abb2",abb2);
jsonObject.put("abb3",abb3);
jsonObject.put("cn",cn);
jsonObject.put("code",code);
jsonObject.put("en",en);
jsonObject.put("full",full);
jsonObject.put("zhou",zhou);
southAArray.add(jsonObject);
}
if(StringUtils.equals("NorthA",zhou)){
jsonObject.put("abb2",abb2);
jsonObject.put("abb3",abb3);
jsonObject.put("cn",cn);
jsonObject.put("code",code);
jsonObject.put("en",en);
jsonObject.put("full",full);
jsonObject.put("zhou",zhou);
northAArray.add(jsonObject);
}
if(StringUtils.equals("Oceania",zhou)){
jsonObject.put("abb2",abb2);
jsonObject.put("abb3",abb3);
jsonObject.put("cn",cn);
jsonObject.put("code",code);
jsonObject.put("en",en);
jsonObject.put("full",full);
jsonObject.put("zhou",zhou);
oceaniaArray.add(jsonObject);
}
map.put("Asia",asiaArray);
map.put("Europe",europeArray);
map.put("Africa",africaArray);
map.put("SouthA",southAArray);
map.put("NorthA",northAArray);
map.put("Oceania",oceaniaArray);
}
}
return map;
}