栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Tree树状数据模糊查询,递归父类与子集(JSON篇)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Tree树状数据模糊查询,递归父类与子集(JSON篇)

废话不多说,直接上代码。

JSON数据的工具类

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;



public class JsonTreeSearchUtil {
    // 名
    private final static  String NAME = "nodeName";
    // 子集
    private final static  String CHILDREN = "children";
    // 其他数据,这里可以设置其他需要的键
    private static  String[] KEYS = new String[]{"pNodeCode","nodeCode","value1","value2"};

    public static void main(String[] args) {
        JSonArray jsonArray = getJsonData();
        System.out.println(jsonArray);
        System.out.println(searchJson(jsonArray,"广州市"));
    }

    
    public static JSonArray searchJson(JSonArray data, String text) {
        JSonArray res = new JSonArray();
        for (Object object : data) {
            JSonObject jsonObject = JSONObject.fromObject(object);
            if(jsonObject.containsKey(CHILDREN) && jsonObject.getJSonArray(CHILDREN).isArray()){
                JSonArray children = jsonObject.getJSonArray(CHILDREN);
                JSonArray searchData = searchJson(children, text);
                JSonObject newJsonObject = new JSonObject();
                newJsonObject.put(NAME,jsonObject.get(NAME));
                if(KEYS.length > 0){
                    for (String str :KEYS) {
                        if(jsonObject.containsKey(str)){
                            newJsonObject.put(str,jsonObject.get(str));
                        }
                    }
                }
                newJsonObject.put(CHILDREN,searchData);
                if (newJsonObject.getString(NAME).contains(text) || searchData.size() > 0) {
                    if(jsonObject.getString(NAME).contains(text)){
                        res.add(jsonObject);
                    }else{
                        res.add(newJsonObject);
                    }
                }
            } else {
                // 确认无子集数据,将该数据直接加入新结果集容器
                if (jsonObject.getString(NAME).contains(text)) {
                    res.add(jsonObject);
                }
            }
        }
        return res;
    }

    // 模拟数据
    private static JSonArray getJsonData(){

        JSonArray jsonArray1 = new JSonArray();

        JSonObject jsonObject001 = new JSonObject();
        jsonObject001.put("nodeCode","001");
        jsonObject001.put("pNodeCode","0");
        jsonObject001.put("value1","广州塔");
        jsonObject001.put("nodeName","广州市");
        JSonObject jsonObject002 = new JSonObject();
        jsonObject002.put("nodeCode","002");
        jsonObject002.put("pNodeCode","0");
        jsonObject002.put("nodeName","深圳市");

        JSonArray jsonArray21 = new JSonArray();
        JSonObject jsonObject001001 = new JSonObject();
        jsonObject001001.put("nodeCode","001001");
        jsonObject001001.put("pNodeCode","001");
        jsonObject001001.put("nodeName","天河区");

        JSonObject jsonObject001002 = new JSonObject();
        jsonObject001002.put("nodeCode","001002");
        jsonObject001002.put("pNodeCode","001");
        jsonObject001002.put("nodeName","荔湾区");

        JSonArray jsonArray22 = new JSonArray();
        JSonObject jsonObject001003 = new JSonObject();
        jsonObject001003.put("nodeCode","002001");
        jsonObject001003.put("pNodeCode","002");
        jsonObject001003.put("nodeName","福田区");

        JSonArray jsonArray31 = new JSonArray();
        JSonObject jsonObject001001001 = new JSonObject();
        jsonObject001001001.put("nodeCode","001001001");
        jsonObject001001001.put("pNodeCode","001001");
        jsonObject001001001.put("nodeName","体育西");
        JSonObject jsonObject001001002 = new JSonObject();
        jsonObject001001002.put("nodeCode","001001001");
        jsonObject001001002.put("pNodeCode","001001");
        jsonObject001001002.put("nodeName","珠江新城");
        jsonObject001001002.put("children",null);
        JSonObject jsonObject001001003 = new JSonObject();
        jsonObject001001003.put("nodeCode","001001002");
        jsonObject001001003.put("pNodeCode","001001");
        jsonObject001001003.put("nodeName","岗顶");
        jsonObject001001003.put("children",null);

        JSonArray jsonArray41 = new JSonArray();
        JSonObject jsonObject001001001001 = new JSonObject();
        jsonObject001001001001.put("nodeCode","001001001001");
        jsonObject001001001001.put("pNodeCode","001001001");
        jsonObject001001001001.put("nodeName","花城汇");
        jsonObject001001001001.put("children",null);
        jsonArray41.add(jsonObject001001001001);
        jsonObject001001001.put("children",jsonArray41);

        jsonArray31.add(jsonObject001001001);
        jsonArray31.add(jsonObject001001002);
        jsonArray31.add(jsonObject001001003);
        jsonObject001001.put("children",jsonArray31);

        jsonArray21.add(jsonObject001001);
        jsonArray21.add(jsonObject001002);
        jsonArray22.add(jsonObject001003);
        jsonObject001.put("children",jsonArray21);
        jsonObject002.put("children",jsonArray22);

        jsonArray1.add(jsonObject001);
        jsonArray1.add(jsonObject002);

        return jsonArray1;
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775019.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号