JSON 快速取值(JAVA )
JSON 快速取值(JAVA )
因为⼯作需要,经常要从复杂的json⾥⾯取值,每次要做写⼀堆JSONObject,然后我就写了⼀个⼩⼯具(刚毕业,写的很菜)。⼯具代码json值的类型有哪些
⽤了hutool的格式转换(Convert)、正则⼯具(ReUtil)和JSON⼯具import  Convert ;import  ReUtil ;import  *;import  Test ;import  ArrayDeque ;import  Arrays ;import  List ;import  Queue ;public  class  JSONValuesUtil {    /**    * 获得价值    *  若传⼊数组,表达式应为:[0].FDetailID.FDETAILID__FFLEX5.FNumber      (tip :传⼊数组 第⼀个元素应为 【数字】)    *  若传⼊json 对象,表达式应为:Model.FEntity[0].FDetailID.FDETAILID__FFLEX5.FNumber      * @param queue  队列    * @param jsonStr json str      * @return {@link String}    */    private  static  String getValue (Queue <String > queue , String jsonStr ) {        //如果队列为空就直接返回null        if  (queue .size () == 0) {            return  null ;        }        JSONTokener jsonTokener = new  JSONTokener (jsonStr ,JSONConfig .create ());        Object json = jsonTokener .nextValue ();        //获取头部属性        String poll = queue .poll ();        // 判断json 类型        if  (json instanceof  JSONArray ) {            JSONArray jsonArray = (JSONArray ) json ;            List <String > all = ReUtil .findAll ("\\[(\\d+)\\]", poll , 1);            if 
(all .size () > 0) {                //如果是数组                String index = all .get (0);                // 判断当前队列是否有值 有就递归没有就返回jsonString                String str = jsonArray .getStr (Convert .toInt (index ));                if  (queue .peek () != null ) {                    return  getValue (queue , str );                } else  {                    return  str ;                }            }            return  null ;        } else  {            JSONObject jsonObject = (JSONObject ) json ;            List <String > all = ReUtil .findAll ("\\[(\\d+)\\]", poll , 1);            if  (all .size () > 0) {                //如果是数组                String index = all .get (0);                // 去除数组字段 (【0】)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 去除数组字段 (【0】)                poll = poll .substring (0, poll .length () - index .length () - 2);                // 判断当前队列是否有值 有就递归没有就返回jsonString                if  (queue .peek () != null ) {                    return  getValue (queue ,jsonObject .getJSONArray (poll ).getStr (Convert .toInt (index )));                } else  {                    return  jsonObject .getJSONArray (poll ).getStr (Convert .toInt (index ));                }            } else  {                // 如果是JSONObject                // 判断当前队列是否有值 有就递归没有就返回jsonString                if  (queue .peek () != null ) {                    return  getValue (queue ,jsonObject .getStr (poll ));                } else  {                    return  jsonObject .getStr (poll );                }            }        }    }    /**    * 格式    *    * @param json      json 字符串    * @param formatStr 格式化字符串(a.b[0].c )    * @return {@link String}    */    public  static  String format (String json , String formatStr ) {        String [] attributes = formatStr .split ("\\.");        if  (attributes .length == 0) {            return  null ;        }        // 把数组转成队列(⽅便操作)        Queue <String > queue = new  ArrayDeque <>(Arrays .asList (attributes ));        return  getValue (queue , json );    }    /**    * 测试    */
    @Test    public  void  test () {        String json = "{\"IsDeleteEntry\":\"true\",\"IsVerifyBaseDataField\":\"false\",\"IsEntryBatchFill\":\"true\",\"ValidateFlag\":\"true\",\"NumberSearch\":\"true\",\        String formatStr = "Model.FEntity[0].FDetailID.FDETAILID__FFLEX5.FNumber";        String format = format (json , formatStr );        System .out .println (format );    }}54555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。