用遞歸方式在JSON中查找對象

Json文件例子:java

 1 {
 2     "type": "Update",
 3     "data": {
 4         "temp": "v",
 5         "Name": "tttt",
 6         "Id": 5,
 7         "template": {
 8             "head": {
 9                 "headtext": "  "
10             },
11             "body": {
12                 "bodytext": {
13                     "1": "123456789"
14                 },
15                 "image": {
16                     "2": 169000
17                 }
18             },
19             "tag": {
20                 "1": {
21                     "image": {
22                         "normal": 179000
23                     },
24                     "back": {
25                         "normal": 180000
26                     },
27                     "state": true
28                 }
29             }
30         }
31     }
32 }

 

代碼:json

  1 package com.test.java;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileReader;
  7 import java.io.IOException;
  8 import java.util.HashSet;
  9 import java.util.Iterator;
 10 
 11 import org.json.JSONException;
 12 import org.json.JSONObject;
 13 
 14 public class FindKeyInJson {
 15 
 16     static String stringInJSON = null;
 17 
 18     public static void main(String[] args) {
 19 
 20         File jsonFile = new File(
 21                 "D:\\jsonexample.txt");
 22         StringBuffer sb = new StringBuffer();
 23         try (BufferedReader br = new BufferedReader(new FileReader(jsonFile))) {
 24             String line = null;
 25             while ((line = br.readLine()) != null) {
 26                 sb.append(line);
 27             }
 28         } catch (FileNotFoundException e) {
 29             // TODO Auto-generated catch block
 30             e.printStackTrace();
 31         } catch (IOException e) {
 32             // TODO Auto-generated catch block
 33             e.printStackTrace();
 34         }
 35 
 36         try {
 37             System.out.println(sb.toString());
 38             JSONObject jo = new JSONObject(sb.toString());
 39             String content = getKeyString(jo, "bodytext");//獲取bodytext字段信息  40             System.out.println(content);
 41         } catch (JSONException e) {
 42             // TODO Auto-generated catch block
 43             e.printStackTrace();
 44         }
 45 
 46     }
 47 
 48     public static String getKeyString(JSONObject json, String key) {
 49         HashSet<String> hSet = new HashSet<String>();
 50 
 51         if (json.has(key)) {
 52             // 在當前的層次中存在指定的key,能夠直接獲取指定key對應的string
 53             try {
 54                 stringInJSON = json.get(key).toString();
 55             } catch (JSONException e) {
 56                 e.printStackTrace();
 57             }
 58         } else {
 59             // 在當前的層次中不存在指定的key,須要往下一個層次查找
 60             hSet = getKeySetOnJSON(json);// 獲取當前層次的全部key
 61             Iterator<String> iterator = hSet.iterator();
 62             while (iterator.hasNext()) {
 63                 String i = iterator.next();
 64                 if (isJSONObject(json, i)) {
 65                     try {
 66                         JSONObject tp = (JSONObject) json.get(i);
 67                         HashSet<String> hSet2 = new HashSet<String>();
 68                         hSet2 = getKeySetOnJSON(tp);
 69                         if (tp.has(key)) {
 70                             stringInJSON = tp.get(key).toString();
 71                             break;
 72                         } else {
 73                             getKeyString(tp, key);// 開始遞歸
 74                         }
 75 
 76                     } catch (JSONException e) {
 77 
 78                         e.printStackTrace();
 79                     }
 80                 }
 81             }
 82         }
 83         return stringInJSON;
 84     }
 85 
 86     public static HashSet<String> getKeySetOnJSON(JSONObject json) {
 87         HashSet<String> hSet = new HashSet<String>();
 88         Iterator<String> iterator = json.keys();
 89         while (iterator.hasNext()) {
 90             String i = iterator.next();
 91             hSet.add(i);
 92         }
 93         return hSet;
 94     }
 95 
 96     public static boolean isJSONObject(JSONObject json, String key) {
 97         boolean flag = false;
 98         try {
 99             try {
100                 JSONObject tp = (JSONObject) json.get(key);
101                 System.out.println(key + " is JSON Object");
102                 flag = true;
103             } catch (JSONException e) {
104                 e.printStackTrace();
105             }
106         } catch (ClassCastException e) {
107             System.out.println(key + " is not JSON Object");
108         }
109         return flag;
110     }
111 
112 }

result:app

 1 {    "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                }            }        }    }}
 2 data is JSON Object
 3 template is JSON Object  4 head is JSON Object
 5 headtext is not JSON Object
 6 tag is JSON Object
 7 1 is JSON Object
 8 image is JSON Object
 9 normal is not JSON Object
10 back is JSON Object
11 normal is not JSON Object
12 state is not JSON Object
13 body is JSON Object 14 temp is not JSON Object
15 Id is not JSON Object
16 Name is not JSON Object
17 type is not JSON Object
18 {"1":"123456789"} //最終結果
相關文章
相關標籤/搜索