N.1 依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>11.0.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> |
这个代码只能获得第一个key的value,不能一下子取出所有的相同的key,所以这个获取数据只能一开始遍历json里面的大数据集合,然后里面的每一条小数据依次条用这些方法。 |
N.2 关系
N.3 demo
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.google.common.base.Objects; import java.io.*; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.ObjectUtils; /** * * 常用JSON工具类 2020/2/26 */ public class JSONUtil { public static void main(String[] args) throws IOException { FileReader filedata = new FileReader("C:\\Users\\HMTX\\Desktop\\tmp\\乡村人员的个人信息.json"); BufferedReader BufferedData = new BufferedReader(filedata); StringBuilder strdata = new StringBuilder(); String line; while ((line = BufferedData.readLine()) != null) { strdata.append(line + '\n'); } BufferedData.close(); System.out.println(getJsonValueBykey(strdata.toString(), "msg")); // 如果json有相同的key,那这个代码只能获得第一个key。而且对于一般的格式 是可以获取的,如果是特别离谱的json 就可能查找不到。 } private static final Log log = LogFactory.getLog(JSONUtil.class); /** * key的value值 */ private static Object value; /** * 判断是否还有子级 */ private static boolean flag = true; /** * 根据key获取value */ public static String getJsonValueBykey(String json, Object key) { log.info("getJsonValueBykey json:" + json); log.info("getJsonValueBykey key:" + key); if (StringUtils.isNotBlank(json) && StringUtils.isNotBlank(key.toString())) { log.info("getJsonValueBykey json and key is not null"); if (isJsonArray(json)) { log.info("json is jsonArray"); JSONArray jsonDataArray = JSONArray.parseArray(json); log.info("jsonDataArray size:" + jsonDataArray.size()); for (int i = 0; i < jsonDataArray.size(); i++) { if (isJsonObject(jsonDataArray.get(i).toString())) { log.info("job json:" + jsonDataArray.get(i)); JSONObject job = jsonDataArray.getJSONObject(i); value = parseJsonString(job.toJSONString(), key).toString(); } } } else if (isJsonObject(json)) { log.warn("json is jsonObject"); value = parseJsonString(json, key).toString(); } return value.toString(); } else { log.error("json and key is null"); return null; } } /** * @描述 从map按动态的key解析值 ,直到解析出想要的值 */ public static Object parseJsonMap(Map.Entry<String, Object> entry, String key) { log.info("value:" + value); if (ObjectUtils.isEmpty(value)) { log.info("value is null"); if (flag) { log.info("entry key:" + entry.getKey()); log.info("key:" + key); if (Objects.equal(entry.getKey(), key)) { value = entry.getValue(); flag = false; } else { log.info("entry value:" + entry.getValue()); //如果是单个map继续遍历 if (entry.getValue() instanceof Map) { log.info("value is map"); if (isJsonMap(entry.getValue().toString())) { LinkedHashMap<String, Object> jsonMap = JSON.parseObject(entry.getValue().toString(), new TypeReference<LinkedHashMap<String, Object>>() { }); for (Map.Entry<String, Object> entry2 : jsonMap.entrySet()) { if (!StringUtils.contains(entry2.getKey(), key)) { log.info("entry2 entry2.getKey() is not key "); value = parseJsonMap(entry2, key); } else { log.warn(" entry2 entry2.getKey() is key "); value = entry2.getValue(); log.info("value:" + value); flag = false; break; } } } } //如果是String就获取它的值 if (entry.getValue() instanceof String) { log.info("entry.getValue() is String:"); log.info("key:" + entry.getKey() + "value:" + entry.getValue()); // parseJsonString(entry.getValue().toString(), key); } //如果是空JSONArray,就返回它的值 if (entry.getValue() instanceof JSONArray && ((JSONArray) entry.getValue()).isEmpty()) { log.info("key" + entry.getKey() + ", value: []"); value = "[]"; log.info("value:" + value); flag = false; } //如果是list就提取出来 if (entry.getValue() instanceof List && ((Collection) entry.getValue()).size() > 0) { List list = (List) entry.getValue(); for (int i = 0; i < list.size(); i++) { parseJsonString(list.get(i).toString(), key); } } } } } log.info("value:" + value); return value; } /** * @描述 从JSON对象中,根据指定key获取值,只拿第一个 */ public static Object parseJsonString(String json, Object key) { log.info("parseJsonString json:" + json); log.info("parseJsonString key:" + key); if (!ObjectUtils.isEmpty(json) && !ObjectUtils.isEmpty(key)) { log.info(" parseJsonString json and key is not null"); if (isJsonMap(json)) { LinkedHashMap<String, Object> jsonMap = JSON.parseObject(json, new TypeReference<LinkedHashMap<String, Object>>() { }); for (Map.Entry<String, Object> entry : jsonMap.entrySet()) { log.info("entry key:" + entry.getKey()); log.info("key:" + key); if (!StringUtils.contains(entry.getKey(), key.toString())) { log.info("is not blank "); value = parseJsonMap(entry, key.toString()); } else { log.warn(" is blank "); value = entry.getValue(); break; } } } return value; } else { log.info("json and key is null"); return null; } } /** * 判断字符串是否可以转化为json对象 */ public static boolean isJsonObject(String content) { // 此处应该注意,不要使用StringUtils.isEmpty(),因为当content为" "空格字符串时,JSONObject.parseObject可以解析成功, // 实际上,这是没有什么意义的。所以content应该是非空白字符串且不为空,判断是否是JSON数组也是相同的情况。 if (StringUtils.isBlank(content)) { return false; } try { JSONObject jsonStr = JSONObject.parseObject(content); return true; } catch (Exception e) { return false; } } /** * 判断字符串是否可以转化为JSON数组 */ public static boolean isJsonArray(String content) { if (StringUtils.isBlank(content)) { return false; } StringUtils.isEmpty(content); try { JSONArray jsonStr = JSONArray.parseArray(content); return true; } catch (Exception e) { return false; } } /** * 判断是否可以按照map动态的key解析值 */ public static boolean isJsonMap(String content) { log.info("content:" + content); if (StringUtils.isBlank(content)) { return false; } StringUtils.isEmpty(content); try { LinkedHashMap<String, Object> jsonMap = JSON.parseObject(content, new TypeReference<LinkedHashMap<String, Object>>() { }); return true; } catch (Exception e) { return false; } } } |