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


    Json文件例子:

     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 }

    代码:

      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:

     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"}  //最终结果
  • 相关阅读:
    WebCollector2.7爬虫框架——在Eclipse项目中配置
    JavaScript 输出
    CSS3 页面中展示邮箱列表点击弹出发送邮件界面
    CSS3 Flex Box(弹性盒子)
    CSS 分页实例
    CSS 图片
    CSS3 用户界面
    CSS3 动画
    CSS3 过渡
    CSS3 3D转换
  • 原文地址:https://www.cnblogs.com/moonpool/p/5710990.html
Copyright © 2020-2023  润新知