1. 程式人生 > >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.  //先引入Jar包   
2.  import org.json.*;
3.  //獲取請求返回值。只能獲取到String  
4.  String response_data=prev.getResponseDataAsString();
5.  //將string 型別返回值構造成Jsonobject 物件 
6.  JSONObject data_obj= new JSONObject(response_data); 
7.  //獲取Json中 first的值
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()方法獲得鍵值對中的值

import org.json.*;  

//獲取請求返回值。只能獲取到String 

String response_data=prev.getResponseDataAsString(); 

JSONObject data_obj= new JSONObject(response_data);

JSONArray apps_array= (JSONArray) ((JSONObject)data_obj.get("data")).get("content");

//獲取第一串json 的patrolJobId

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中

import org.json.*;  

//獲取請求返回值。只能獲取到String 

String response_data=prev.getResponseDataAsString(); 

JSONObject data_obj= new JSONObject(response_data);

JSONArrayapps_array= (JSONArray)((JSONObject)data_obj.get("data")).get("content"); 

ArrayList list =new ArrayList();  
//外層迴圈獲取JsonArrary的每一串Json
for(int j=0;j<apps_array.length();j++){  
    String apps_str1=apps_array.get(j).get("patrolUnitList").toString();  
    JSONArray apps_array1= new JSONArray(apps_str1);  
    for (int i=0;i<apps_array1.length();i++){  
        //內層迴圈查詢每個Json裡的iBeaconUniqueCode值
        JSONObject app_obj= new JSONObject(apps_array1.get(i).toString());  

        String iBeaconUniqueCode1= app_obj.get("iBeaconUniqueCode").toString();  
        //將iBeaconUniqueCode值新增到ArrayList中
        list.add(iBeaconUniqueCode1);  

        }  
    }  

String[] iBeaconUniqueCode= new String[list.size()];    

for(int i=0;i<list.size();i++){  

    iBeaconUniqueCode[i]=list.get(i);   

     }  

vars.put("iBeaconUniqueCode", Arrays.toString(iBeaconUniqueCode));