json实例
{
"success": true,
"code": 200,
"message": "调用成功",
"data": {
"nodeResults": [{
"success": true,
"result": {
"success": true,
"score": 240.0,
"hitRules": [{
"success": true,
"score": 60.0
}, {
"success": true,
"score": 61.0
}, {
"success": true,
"score": 66.0
}, {
"success": true,
"score": 610.0
}]
}
}, {
"success": true,
"result": {
"success": true,
"score": 120.0,
"hitRules": [{
"success": true,
"score": 50.0
}, {
"success": true,
"score": 90.0
}]
}
}]
}
}
现在要获取score的值,路径为data->nodeResults->result->hitRules->score
采用递归算法,层层分析json,自定义一个简单的表达式,对象就直接写属性名,数组的属性名后面加上[]
data.nodeResults[].result.hitRules[].score
算法代码如下:
private static void analysisJSON(String el,String json){
String[] els = el.split("\\.");
JSONArray array ;
JSONObject jsonObject;
String s = "";
if(els.length==1){ s = el;}
else {
for (int i = 1; i < els.length; i++) {
s = s + els[i] + ".";
}
}
if("".equals(s)) return;
JSONObject temp = new JSONObject();
if (els.length==0) return;
if(els[0].endsWith("[]")){
jsonObject = JSONObject.parseObject(json);
if(jsonObject.get(els[0].substring(0,els[0].length()-2))!=null) {
array = JSONArray.parseArray(jsonObject.get(els[0].substring(0, els[0].length() - 2)).toString());
for (int i = 0; i < array.size(); i++) {
analysisJSON(s.substring(0, s.length() - 1), ((JSONObject) array.get(i)).toJSONString());
}
}
}else {
try {
jsonObject = JSONObject.parseObject(json);
if (jsonObject.get(els[0]) != null) {
if(els.length==1) {
System.out.println("已找到匹配的字段------>"+jsonObject.get(els[0]).toString());
}else{
analysisJSON(s.substring(0, s.length() - 1), jsonObject.get(els[0]).toString());}
}
}catch (Exception e){
}
}
}
main方法调用端:
public static void main(String[] args) {
String text = "";//json内容
analysisJSON("data.nodeResults[].result.hitRules[].score",text );
}
运行结果如下:
已找到匹配的字段------>60.0
已找到匹配的字段------>61.0
已找到匹配的字段------>66.0
已找到匹配的字段------>610.0
已找到匹配的字段------>50.0
已找到匹配的字段------>90.0
加个返回List
private static List analysisJSON(String el,String json,List res){
String[] els = el.split("\\.");
JSONArray array ;
JSONObject jsonObject;
String s = "";
if(els.length==1){ s = el;}
else {
for (int i = 1; i < els.length; i++) {
s = s + els[i] + ".";
}
}
if("".equals(s)) return null;
JSONObject temp = new JSONObject();
if (els.length==0) return res;
if(els[0].endsWith("[]")){
jsonObject = JSONObject.parseObject(json);
if(jsonObject.get(els[0].substring(0,els[0].length()-2))!=null) {
array = JSONArray.parseArray(jsonObject.get(els[0].substring(0, els[0].length() - 2)).toString());
for (int i = 0; i < array.size(); i++) {
analysisJSON(s.substring(0, s.length() - 1), ((JSONObject) array.get(i)).toJSONString(),res);
}
}
}else {
try {
jsonObject = JSONObject.parseObject(json);
if (jsonObject.get(els[0]) != null) {
if(els.length==1) {
res.add(jsonObject.get(els[0]).toString());
System.out.println("已找到匹配的字段------>"+jsonObject.get(els[0]).toString());
}else{
analysisJSON(s.substring(0, s.length() - 1), jsonObject.get(els[0]).toString(),res);
}
}
}catch (Exception e){
}
}
return res;
}
调用时传入一个List,接收命中字段
List res = analysisJSON("data.nodeResults[].result.hitRules[].score", sbf.toString(),new ArrayList<>() );
System.out.println(JSONObject.toJSONString(res));
后台打印输出
已找到匹配的字段------>60.0
已找到匹配的字段------>61.0
已找到匹配的字段------>66.0
已找到匹配的字段------>610.0
已找到匹配的字段------>50.0
已找到匹配的字段------>90.0
["60.0","61.0","66.0","610.0","50.0","90.0"]