有时我们想通过接口响应数据中的默写条件进行提取对应的字段,这时,就可以用到json条件提取,可以提取对应的值进行使用或迭代。
接口请求地址:
sh.lianjia.com/api/newhouserecommend?type=1&query=https%3A%2F%2Fsh.lianjia.com%2Fershoufang%2F%3Futm_source%3Dbaidu%26utm_medium%3Dpinzhuan%26utm_term%3Dbiaoti%26utm_content%3Dbiaotimiaoshu%26utm_campaign%3Dsousuo%26ljref%3Dpc_sem_baidu_ppzq_x
接口响应数据:
{
"errno": 0,
"uniq_id": "010A1A64181308FE356F0175ABDB6D34",
"data": [
{
"outlet_center_tel": "",
"cover_pic": "https:\/\/image1.ljcdn.com\/hdic-resblock\/prod-09d53f11-de84-4cdd-9fe9-e240310e985fphpnmDROp",
"project_name": "zxgcbjbjx",
"project_desc": "品质建筑 交通便捷",
"bizcircle_id": "611100121",
"show_price_confirm_time": "0天",
"discount_realpay": "0",
"special_tag": "",
"resblock_alias": "春申大厦",
"price": "41000",
"checkin_start_date": null,
"id": "5020035843156409",
"min_frame_area": "0",
"decoration": "毛坯",
"carRatio": "1:",
"address_remark": "春申路3555号",
"allowance": "0",
"tags": [
],
"permit_all_ready": "1",
"has_virtual_view": "0",
"property_address": "",
"build_id": "5020035843156409",
"process_status": "3",
"full_spell": "zhongxuguangchang",
"evaluate": [
],
……
提取 "decoration": "毛坯"的所有resblock_alias值:
$..data[?(@.decoration=="毛坯")].resblock_alias
提取结果:
Result[0]=春申大厦
Result[1]=塘和家园栀子锦苑,樾山明月
Result[2]=龙茗路商铺
Result[3]=星月珂汇园
添加Json提取器提取(放置采样器下级):
Variable names:test
JSONPath Expression:$..data[?(@.decoration=="毛坯")].resblock_alias
0随机;n取第几个匹配值;-1匹配所有,后续引用用 变量名_N 取第N个值
Match Numbers:-1
添加debug采样器,查看提取结果:
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@77ed38c5
START.HMS=113707
START.MS=1577158627509
START.YMD=20191224
TESTSTART.MS=1577160022883
__jm__线程组__idx=0
__jmeter.U_T__=线程组 1-1
test_1=龙茗路商铺
test_2=春申大厦
test_3=塘和家园栀子锦苑,樾山明月
test_4=星月珂汇园
test_matchNr=4
如果想使用其中1个提取值,可以用提取变量test拼接第几个值_几,如:test_1、test_5进行使用
如果想迭代提取出来的多个值,可以添加ForEach控制器进行迭代,共提取出来4个值,我们迭代4次,设置如下:
查看结果树,发现被引用变量的采样器,最终被迭代了4次