public static void putAllIdToMap(String json, String name, String id) { String value = ""; String key = ""; Map<String, Object> map = (Map<String, Object>)JsonUtils.jsonToMap(json); for (Entry<String, Object> key1 : map.entrySet()) { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(JsonUtils.object2Json(key1.getValue())); if (jsonElement.isJsonObject()) { JsonObject jsonObject = jsonElement.getAsJsonObject(); String jsonstr = jsonObject.toString(); putAllIdToMap(jsonstr, name, id); } else if (jsonElement.isJsonArray()) { List<Map<String, Object>> liatMap = (List<Map<String, Object>>)key1.getValue(); for (Map<String, Object> map2 : liatMap) { String jsonstr = JsonUtils.object2Json(map2); putAllIdToMap(jsonstr, name, id); } } else { if (key1.getKey().equals(name)) { if (!"".equals(key1.getValue()) && null != key1.getValue()) { key = key1.getValue().toString(); } } if (key1.getKey().equals(id)) { if (!"".equals(key1.getValue()) && null != key1.getValue()) { value = key1.getValue().toString(); } } if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value)) { idMap.put(key, value); break; } } } }
注:根据name和ID,找出以name为key id的值为value的键值对,由于获取到的json可能是空、Object、JsonArray等,所以先判断,在使用递归解决多层嵌套问题。