废话不多说,直接上代码。
JSON数据的工具类
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Niz
* 模糊搜索JSON树状数据
* 返回查询到的数据的递归上级和递归下级
*
*/
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,"广州市"));
}
/**
* 模糊搜索
* @param data 树状数据
* @param text 搜索名
* @return
*/
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;
}
}
|