您的位置:首页 > Web前端 > JavaScript

用递归方式在JSON中查找对象

2016-07-27 14:29 447 查看
Json文件例子:

{
"type": "Update",
"data": {
"temp": "v",
"Name": "tttt",
"Id": 5,
"template": {
"head": {
"headtext": "  "
},
"body": {
"bodytext": {
"1": "123456789"
},
"image": {
"2": 169000
}
},
"tag": {
"1": {
"image": {
"normal": 179000
},
"back": {
"normal": 180000
},
"state": true
}
}
}
}
}


代码:

package com.test.java;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

public class FindKeyInJson {

static String stringInJSON = null;

public static void main(String[] args) {

File jsonFile = new File(
"D:\\jsonexample.txt");
StringBuffer sb = new StringBuffer();
try (BufferedReader br = new BufferedReader(new FileReader(jsonFile))) {
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
System.out.println(sb.toString());
JSONObject jo = new JSONObject(sb.toString());
 String content = getKeyString(jo, "bodytext");//获取bodytext字段信息
             System.out.println(content);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static String getKeyString(JSONObject json, String key) {
HashSet<String> hSet = new HashSet<String>();

if (json.has(key)) {
// 在当前的层次中存在指定的key,可以直接获取指定key对应的string
try {
stringInJSON = json.get(key).toString();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
// 在当前的层次中不存在指定的key,需要往下一个层次查找
hSet = getKeySetOnJSON(json);// 获取当前层次的所有key
Iterator<String> iterator = hSet.iterator();
while (iterator.hasNext()) {
String i = iterator.next();
if (isJSONObject(json, i)) {
try {
JSONObject tp = (JSONObject) json.get(i);
HashSet<String> hSet2 = new HashSet<String>();
hSet2 = getKeySetOnJSON(tp);
if (tp.has(key)) {
stringInJSON = tp.get(key).toString();
break;
} else {
 getKeyString(tp, key);// 开始递归
}

} catch (JSONException e) {

e.printStackTrace();
}
}
}
}
return stringInJSON;
}

public static HashSet<String> getKeySetOnJSON(JSONObject json) {
HashSet<String> hSet = new HashSet<String>();
Iterator<String> iterator = json.keys();
while (iterator.hasNext()) {
String i = iterator.next();
hSet.add(i);
}
return hSet;
}

public static boolean isJSONObject(JSONObject json, String key) {
boolean flag = false;
try {
try {
JSONObject tp = (JSONObject) json.get(key);
System.out.println(key + " is JSON Object");
flag = true;
} catch (JSONException e) {
e.printStackTrace();
}
} catch (ClassCastException e) {
System.out.println(key + " is not JSON Object");
}
return flag;
}

}


result:

{    "type": "Update",    "data": {        "temp": "v",        "Name": "tttt",        "Id": 5,        "template": {            "head": {                "headtext": "  "            },            "body": {                "bodytext": {                    "1": "123456789"                },                "image": {                    "2": 169000                }            },            "tag": {                "1": {                    "image": {                        "normal": 179000                    },                    "back": {                        "normal": 180000                    },                    "state": true                }            }        }    }}
data is JSON Object
template is JSON Object
 head is JSON Object
headtext is not JSON Object
tag is JSON Object
1 is JSON Object
image is JSON Object
normal is not JSON Object
back is JSON Object
normal is not JSON Object
state is not JSON Object
body is JSON Object
 temp is not JSON Object
Id is not JSON Object
Name is not JSON Object
type is not JSON Object
{"1":"123456789"}  //最终结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: