废话不多说,直接上代码。
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;
}
}


