当前位置:   article > 正文

beanshell解析json(从简单到复杂)_beanshell 解析json

beanshell 解析json

使用beanshell 解析单层Json:

Json 数据如下:

{
   "status":200,
   "code": 0,
   "message": "成功",
   "data": {
       "last": false,
       "totalPages": 7,
       "numberOfElements": 3,
       "first": true,
       "totalElements": 64,
       "size": 10
             }
}

则对于单层Json 则使用beanshell 如此解析:

思路:

1.接收返回值并定义为String 类型

2.将接收到的Sting 转换为JSONObject

3. 调用JSON对象中get()方法获得键值对中的值

 

  1. 1. //先引入Jar包
  2. 2. import org.json.*;
  3. 3. //获取请求返回值。只能获取到String
  4. 4. String response_data=prev.getResponseDataAsString();
  5. 5. //将string 类型返回值构造成Jsonobject 对象
  6. 6. JSONObject data_obj= new JSONObject(response_data);
  7. 7. //获取Json中 first的值
  8. 8. String apps_str= data_obj.get("data").get("first").toString();


使用beanshell 解析多层Json:

 

{
         "status":200,
         "code":0,
         "message":"成功",
         "data":{
                   "last":false,
                   "totalPages":7,
                   "numberOfElements":3,
                   "first":true,
                   "totalElements":64,
                   "size":10,
                   "content":[
{
                                     "patrolJobId":"ff80808160058467016028b15b120cd0",
                                     "pathName":"安管B班12月巡更",
                                     "expectedStartTime":"2017-12-06T13:00:00+0800",
                                     "expectedEndTime":"2017-12-06T19:00:00+0800",
                                     "currentSystemTime":"2017-12-06T16:18:24+0800"
                            },
                            {
                                     "patrolJobId":"ff80808160058467016028b15b130cd1",
                                     "pathName":"安管B班12月巡更",
                                     "expectedStartTime":"2017-12-06T19:00:00+0800",
                                     "expectedEndTime":"2017-12-07T01:00:00+0800",
                                     "currentSystemTime":"2017-12-06T16:18:25+0800"
                            }
                   ]
         },
         "number":0
}


则对于多层Json 则使用beanshell 如此解析:

思路:

1.接收返回值并定义为String 类型

2.将接收到的Sting 转换为JSONObject

3.获取jsonarrary

4.调用Jsonarray对象中get()方法获得键值对中的值

 

  1. import org.json.*;
  2. //获取请求返回值。只能获取到String
  3. String response_data=prev.getResponseDataAsString();
  4. JSONObject data_obj= new JSONObject(response_data);
  5. JSONArray apps_array= (JSONArray) ((JSONObject)data_obj.get("data")).get("content");
  6. //获取第一串json 的patrolJobId
  7. String patrolJobId= ((JSONObject)apps_array.get(0)).get("patrolJobId").toString();


使用beanshell 获取Json中所有该键(iBeaconUniqueCode)的值:

 

{
         "status":200,
         "code":0,
         "message":"成功",
         "data":{
                   "last":false,
                   "totalPages":7,
                   "numberOfElements":3,
                   "first":true,
                   "totalElements":64,
                   "size":10,
                   "content":[{
                                     "patrolJobId":"ff80808160058467016028b15b120cd0",
                                     "pathName":"安管B班12月巡更",
                                     "expectedStartTime":"2017-12-06T13:00:00+0800",
                                     "expectedEndTime":"2017-12-06T19:00:00+0800",
                                     "currentSystemTime":"2017-12-06T16:18:24+0800",
                                     "patrolUnitList":[ 
                    { 
                        "signinTime":"2017-12-06T14:37:54+0800", 
                       "unitName":"1座 1楼 3号铺", 
                        "iBeaconUniqueCode": "GY-XC010103", 
                        "signedIn":true 
                    }, 
                    { 
                        "unitName":"1座 1楼 8号铺", 
                       "iBeaconUniqueCode": "GY-XC010108", 
                        "signedIn":false 
                    }
                                    ] 
                            },
                            {
                                     "patrolJobId":"ff80808160058467016028b15b130cd1",
                                     "pathName":"安管B班12月巡更",
                                     "expectedStartTime":"2017-12-06T19:00:00+0800",
                                     "expectedEndTime":"2017-12-07T01:00:00+0800",
                                     "currentSystemTime":"2017-12-06T16:18:25+0800",
                                     "patrolUnitList":[ 
                    { 
                        "unitName":"1座 1楼 3号铺", 

                        "iBeaconUniqueCode": "GY-XC010103", 

                        "signedIn":false 
                    }, 
                    { 
                        "unitName":"1座 1楼 8号铺", 
                        "iBeaconUniqueCode": "GY-XC010108", 
                        "signedIn":false 
                    } 
                ] 
                            }
                   ]
         },
         "number":0
}


思路:

new一个ArraryList,因为iBeaconUniqueCode存在于2层json 中,所以需要2层循环查找iBeaconUniqueCode,最后使用get()方法获取该值,并添加到ArraryList中

  1. import org.json.*;
  2. //获取请求返回值。只能获取到String
  3. String response_data=prev.getResponseDataAsString();
  4. JSONObject data_obj= new JSONObject(response_data);
  5. JSONArrayapps_array= (JSONArray)((JSONObject)data_obj.get("data")).get("content");
  6. ArrayList list =new ArrayList();
  7. //外层循环获取JsonArrary的每一串Json
  8. for(int j=0;j<apps_array.length();j++){
  9. String apps_str1=apps_array.get(j).get("patrolUnitList").toString();
  10.     JSONArray apps_array1= new JSONArray(apps_str1);
  11.     for (int i=0;i<apps_array1.length();i++){
  12.         //内层循环查找每个Json里的iBeaconUniqueCode值
  13.         JSONObject app_obj= new JSONObject(apps_array1.get(i).toString());
  14.         String iBeaconUniqueCode1= app_obj.get("iBeaconUniqueCode").toString();
  15.         //将iBeaconUniqueCode值添加到ArrayList中
  16.      list.add(iBeaconUniqueCode1);
  17. }
  18. }
  19. String[] iBeaconUniqueCode= new String[list.size()];
  20. for(int i=0;i<list.size();i++){
  21.     iBeaconUniqueCode[i]=list.get(i);
  22. }
  23. vars.put("iBeaconUniqueCode", Arrays.toString(iBeaconUniqueCode));

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/1014183
推荐阅读
相关标签
  

闽ICP备14008679号