当前位置:   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. //先引入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));

---------------------

作者:城市的柏油路太硬

来源:CSDN

原文:https://blog.csdn.net/qq_34309753/article/details/78753307?utm_source=copy

版权声明:本文为博主原创文章,转载请附上博文链接!

原文链接:https://www.cnblogs.com/zgq123456/p/9782745.html

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1014167
推荐阅读
相关标签
  

闽ICP备14008679号