@Autowired
private TestService testService;
@RequestMapping("/list/test")
public R test() {
List<TestEntity> tax =
testService.list(new LambdaQueryWrapper<TestEntity>().eq(TestEntity::getSettingCode, "Tax"));
String collect = tax.stream().map(TestEntity::getJsonValue).collect(Collectors.joining(","));
String jsonValue = "[" + collect + "]";
String filedNames = "countryCode,countryName";
JSONArray objects = JSONUtil.parseArray(jsonValue);
List<Map> jsonList = JSONUtil.toList(objects, Map.class);
List<Map> resultList = new ArrayList<>();
jsonList.forEach(map -> {
Map resultMap = new HashMap();
for (String filedName : filedNames.split(",")) {
resultMap.put(filedName, map.get(filedName));
}
resultList.add(resultMap);
});
return R.ok().put("data", resultList);
}
@RequestMapping("/list/test2")
public R test2() {
String jsonValue = "{\"msg\":\"success\",\"code\":0,\"data\":[{\n" + "\"name\":\"xiaogou\",\n"
+ "\"age\":\"18\",\n" + "\"sex\":\"男\",\n" + "\"countryCode\":\"CN\",\n" + "\"countryName\":\"中国\"\n"
+ "},{\n" + "\"name\":\"小feng\",\n" + "\"age\":\"18\",\n" + "\"sex\":\"女\",\n" + "\"countryCode\":\"GB\",\n"
+ "\"countryName\":\"英国\"\n" + "}]}";
String headKeys = "data";
String filedNames = "name,age,sex";
String[] headKeySplit = headKeys.split(",");
JSONObject objects = JSONUtil.parseObj(jsonValue);
JSONArray jsonArray = null;
for (String headKey : headKeySplit) {
if (headKey.equals(headKeySplit[headKeySplit.length - 1])) {
jsonArray = JSONUtil.parseArray(objects.get(headKey));
} else {
objects = JSONUtil.parseObj(objects.get(headKey));
}
}
List<Map> jsonList = JSONUtil.toList(jsonArray, Map.class);
List<Map> resultList = new ArrayList<>();
jsonList.forEach(map -> {
Map resultMap = new HashMap();
for (String filedName : filedNames.split(",")) {
resultMap.put(filedName, map.get(filedName));
}
resultList.add(resultMap);
});
return R.ok().put("data", resultList);
}
@RequestMapping("/list/test3")
public R test3() {
String jsonValue = "{\"msg\":\"success\",\"code\":0,\"data\":[{\n" + "\"name\":\"xiaogou\",\n"
+ "\"age\":\"18\",\n" + "\"sex\":\"男\",\n" + "\"countryCode\":\"CN\",\n" + "\"countryName\":\"中国\",\n"
+ "\"school\":[\n" + "{\n" + "\"name\":\"渡头小学\",\n" + "\"code\":\"001\",\n" + "\"age\":12\n" + "}\n" + "\n"
+ "]\n" + "},{\n" + "\"name\":\"小feng\",\n" + "\"age\":\"18\",\n" + "\"sex\":\"女\",\n"
+ "\"countryCode\":\"GB\",\n" + "\"countryName\":\"英国\",\n" + "\"school\":[\n" + "{\n"
+ "\"name\":\"鄱阳中学\",\n" + "\"code\":\"002\",\n" + "\"age\":15\n" + "}\n" + "]\n" + "}]}";
String headKeys = "data,school";
String filedNames = "name,code";
String[] headKeySplit = headKeys.split(",");
JSONObject objects = JSONUtil.parseObj(jsonValue);
JSONArray jsonArray = null;
List<Map> resultList = new ArrayList<>();
jsonArray = JSONUtil.parseArray(objects.get(headKeySplit[0]));
jsonArray.stream().forEach(json -> {
JSONArray jsonArray1 = JSONUtil.parseArray(JSONUtil.parseObj(json).get(headKeySplit[1]));
List<Map> jsonList = JSONUtil.toList(jsonArray1, Map.class);
jsonList.forEach(map -> {
Map resultMap = new HashMap();
for (String filedName : filedNames.split(",")) {
resultMap.put(filedName, map.get(filedName));
}
resultList.add(resultMap);
});
});
return R.ok().put("data", resultList);
}
json多次级别的转换
于 2022-02-22 23:03:49 首次发布