zoukankan      html  css  js  c++  java
  • 如何获取json某一级节点的数据

    如何获取json某一级节点的数据

    最近做项目有获取和设置固定格式某一级节点值的需求。但是要一级一级地取对于多级的结构来说代码过于冗余且重复,于是写了个递归的方法根据json路径完成值的定点操作。废话不多说直接贴代码。

        /**
         * 通用的 通过json路径找到json值
         * @param jsonObject 要取值的json对象
         * @param path 对象路径
         * @return 对象值列表 由于可能存在A.B.C路径中B为列表的情况,所以结果可能有多个
         */
        public static List<Object> getJsonFieldValue(JSONObject jsonObject, String path) {
            List<String> keyWordList = new ArrayList(Arrays.asList(path.split("\.")));
            List<Object> list = new ArrayList<>();
            String key = keyWordList.get(0);
            Object object = jsonObject.get(key);
            keyWordList.remove(0);
            if (keyWordList.isEmpty()) {
                if (null != object) {
                    list.add(object);
                }
                return list;
            }
    
            String subPath = StringUtils.join(keyWordList, ".");
            if (object instanceof JSONArray) {
                JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
                if (jsonArray.isEmpty()) {
                    return new ArrayList<>();
                }
                jsonArray.forEach(e -> list.addAll(getJsonFieldValue(JSON.parseObject(JSON.toJSONString(e)), subPath)));
            }else if(object instanceof JSONObject){
                JSONObject subJsonObject = JSONObject.parseObject(JSON.toJSONString(object));
                list.addAll(getJsonFieldValue(JSON.parseObject(JSON.toJSONString(subJsonObject)), subPath));
            }
            return list;
        }
    
        /**
         * 通用的 通过json路径找到json值
         * @param jsonObject 要取值的json对象
         * @param path 对象路径
         * @param value 要设置的值
         * @return 修改后的对象
         */
        public static Object setJsonFieldValue(JSONObject jsonObject, String path, Object value) {
            List<String> keyWordList = new ArrayList(Arrays.asList(path.split("\.")));
            String key = keyWordList.get(0);
            keyWordList.remove(0);
            //如果关键词为空,说明此处为目标点,设置值
            if (keyWordList.isEmpty()) {
                jsonObject.fluentPut(key,value);
                return jsonObject;
            }
            //关键词不为空,取出子jsonObject进行递归
            Object object = jsonObject.get(key);
    
            String subPath = StringUtils.join(keyWordList, ".");
            if (object instanceof JSONArray) {
                JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
                if (jsonArray.isEmpty()) {
                    //说明不是目标点,原样返回
                    return object;
                }
                JSONArray newArray = new JSONArray();
                jsonArray.forEach(e -> newArray.add(setJsonFieldValue(JSON.parseObject(JSON.toJSONString(e)), subPath,value)));
                jsonObject.fluentPut(key,newArray);
            }else if(object instanceof JSONObject){
                JSONObject subJsonObject = JSONObject.parseObject(JSON.toJSONString(object));
                jsonObject.fluentPut(key,setJsonFieldValue(JSON.parseObject(JSON.toJSONString(subJsonObject)), subPath,value));
            }
            return jsonObject;
        }
  • 相关阅读:
    BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office新的App模型
    android Notification 的使用(锁定通知栏)
    Android中经常使用的工具类02
    【python系列】python画报表(Chartkick、Flask)(附中文乱码解决方式)
    Nginx 笔记与总结(14)expires 缓存设置
    数据分析师、数据科学家常见的77个面试问题
    数据分析师、数据科学家常见的77个面试问题
    常见数据分析方法汇总
    常见数据分析方法汇总
    机器学习、数据挖掘、人工智能、统计模型这么多概念有何差异
  • 原文地址:https://www.cnblogs.com/CodingJacob/p/12431825.html
Copyright © 2011-2022 走看看