4 JOSN提取器和JSON JMSPath提取器比较
为了更好让大家理解JOSN提取器和JSON JMSPath提取器,我们在网上建立一个JSON文件food.json。内容如下。
{ "store": { "food": [ { "category": "零食", "name": "黄飞鸿花生", "desc": "黄飞红 麻辣花生210g*2袋 每日坚果炒货休闲网红囤货零食小吃下酒菜花生米", "price":28.8 }, { "category": "零食", "name": "良品铺子", "desc": "良品铺子 经典坚果礼盒8袋装/1310g食食有爱 每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃", "date":"2022-2-4", "price":108.00 }, { "category": "零食", "name": "三只松鼠每日坚果", "desc": "三只松鼠每日坚果 750g/30袋 中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果", "price":149.00 }, { "category": "零食", "name": "百草味", "desc": "百草味 休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点 原味肉松饼1000g/箱", "price":34.9 }, ], "book": { "name": "全栈软件测试工程师宝典", "author": "顾翔", "price": 168.00 } }, "expensive": 70 }
1)把放到Tomcat服务器中
2)建立一个HTTP请求,如图29所示(本节的内容见本书配套代码json.jmx)。
图29 food.json TTP请求
3)建立JSON提取器,如图30所示。
图30 food.json JSON提取器
- Names of created variables:var1;var2;var3;var4;var5;var6;var7;var8;var9;var10;var11;var12。
- JSON Path Expressions:.store.food[∗].name;..name;.store.∗;.store..price;..food[3];..food[-3];..food[1,2];..food[1:3];..food[−3:];..food[?(@.date)];..food[?(@.price<100)];..food[?(@.price<=$['expensive'])]。
- Match No. (0 for Random):-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1。
- Default Values:null;null;null;null;null;null;null;null;null;null;null;null。
4)然后得到如下结果。
var10_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子 经典坚果礼盒8袋装\/1310g食食有爱 每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var10_matchNr=1 var11_1={"name":"黄飞鸿花生","category":"零食","price":28.8,"desc":"黄飞红 麻辣花生210g*2袋 每日坚果炒货休闲网红囤货零食小吃下酒菜花生米"} var11_2={"name":"百草味","category":"零食","price":34.9,"desc":"百草味 休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点 原味肉松饼1000g\/箱"} var11_matchNr=2 var12_1={"name":"黄飞鸿花生","category":"零食","price":28.8,"desc":"黄飞红 麻辣花生210g*2袋 每日坚果炒货休闲网红囤货零食小吃下酒菜花生米"} var12_2={"name":"百草味","category":"零食","price":34.9,"desc":"百草味 休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点 原味肉松饼1000g\/箱"} var12_matchNr=2 var1_1=黄飞鸿花生 var1_2=良品铺子 var1_3=三只松鼠每日坚果 var1_4=百草味 var1_matchNr=4 var2_1=黄飞鸿花生 var2_2=良品铺子 var2_3=三只松鼠每日坚果 var2_4=百草味 var2_5=全栈软件测试工程师宝典 var2_matchNr=5 var3_1=[{"category":"零食","name":"黄飞鸿花生","desc":"黄飞红 麻辣花生210g*2袋 每日坚果炒货休闲网红囤货零食小吃下酒菜花生米","price":28.8},{"category":"零食","name":"良品铺子","desc":"良品铺子 经典坚果礼盒8袋装\/1310g食食有爱 每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃","date":"2022-2-4","price":108.0},{"category":"零食","name":"三只松鼠每日坚果","desc":"三只松鼠每日坚果 750g\/30袋 中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果","price":149.0},{"category":"零食","name":"百草味","desc":"百草味 休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点 原味肉松饼1000g\/箱","price":34.9}] var3_2={"name":"全栈软件测试工程师宝典","author":"顾翔","price":168.0} var3_matchNr=2 var4_1=28.8 var4_2=108.0 var4_3=149.0 var4_4=34.9 var4_5=168.0 var4_matchNr=5 var5_1={"name":"百草味","category":"零食","price":34.9,"desc":"百草味 休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点 原味肉松饼1000g\/箱"} var5_matchNr=1 var6_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子 经典坚果礼盒8袋装\/1310g食食有爱 每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var6_matchNr=1 var7_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子 经典坚果礼盒8袋装\/1310g食食有爱 每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var7_2={"name":"三只松鼠每日坚果","category":"零食","price":149.0,"desc":"三只松鼠每日坚果 750g\/30袋 中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果"} var7_matchNr=2 var8_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子 经典坚果礼盒8袋装\/1310g食食有爱 每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var8_2={"name":"三只松鼠每日坚果","category":"零食","price":149.0,"desc":"三只松鼠每日坚果 750g\/30袋 中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果"} var8_matchNr=2 var9_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子 经典坚果礼盒8袋装\/1310g食食有爱 每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var9_2={"name":"三只松鼠每日坚果","category":"零食","price":149.0,"desc":"三只松鼠每日坚果 750g\/30袋 中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果"} var9_3={"name":"百草味","category":"零食","price":34.9,"desc":"百草味 休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点 原味肉松饼1000g\/箱"} var9_matchNr=3
5)建立如表3对应表格。
表3 food.json JSON提取器获取数据
JSON Path |
值 |
含义 |
$.store.food[*].name |
var1_1=黄飞鸿花生 var1_2=良品铺子 var1_3=三只松鼠每日坚果 var1_4=百草味 var1_matchNr=4 |
所有food的name值 |
$..name |
var2_1=黄飞鸿花生 var2_2=良品铺子 var2_3=三只松鼠每日坚果 var2_4=百草味 var2_5=全栈软件测试工程师宝典 var2_matchNr=5 |
所有的name值 |
$.store.* |
var3_1=[{"category":"零食","name":"黄飞鸿花生","desc":"黄飞红麻辣花生210g*2袋每日坚果炒货休闲网红囤货零食小吃下酒菜花生米","price":28.8},{"category":"零食","name":"良品铺子","desc":"良品铺子经典坚果礼盒8袋装\/1310g食食有爱每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃","date":"2022-2-4","price":108.0},{"category":"零食","name":"三只松鼠每日坚果","desc":"三只松鼠每日坚果 750g\/30袋中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果","price":149.0},{"category":"零食","name":"百草味","desc":"百草味休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点原味肉松饼1000g\/箱","price":34.9}] var3_2={"name":"全栈软件测试工程师宝典","author":"顾翔","price":168.0} var3_matchNr=2 |
store内所有的数据 |
$.store..price |
var4_1=28.8 var4_2=108.0 var4_3=149.0 var4_4=34.9 var4_5=168.0 var4_matchNr=5 |
所有的价格 |
$..food[3] |
var5_1={"name":"百草味","category":"零食","price":34.9,"desc":"百草味休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点原味肉松饼1000g\/箱"} var5_matchNr=1 |
标号为3的food数据(第一个标号为) |
$..food[-3] |
var6_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子经典坚果礼盒8袋装\/1310g食食有爱每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var6_matchNr=1 |
倒数第3个food数据 |
$..food[1,2] |
var7_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子经典坚果礼盒8袋装\/1310g食食有爱每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var7_2={"name":"三只松鼠每日坚果","category":"零食","price":149.0,"desc":"三只松鼠每日坚果 750g\/30袋中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果"} var7_matchNr=2 |
标号为1到2的food数据 |
$..food[1:3] |
var8_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子经典坚果礼盒8袋装\/1310g食食有爱每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var8_2={"name":"三只松鼠每日坚果","category":"零食","price":149.0,"desc":"三只松鼠每日坚果 750g\/30袋中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果"} var8_matchNr=2 |
标号为1和2的food数据(不含3) |
$..food[-3:] |
var9_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子经典坚果礼盒8袋装\/1310g食食有爱每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var9_2={"name":"三只松鼠每日坚果","category":"零食","price":149.0,"desc":"三只松鼠每日坚果 750g\/30袋中秋送礼坚果礼盒零食大礼包儿童孕妇节日混合干果腰果夏威夷果核桃仁开心果"} var9_3={"name":"百草味","category":"零食","price":34.9,"desc":"百草味休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点原味肉松饼1000g\/箱"} var9_matchNr=3 |
倒数3个food数据 |
$..food[?(@.date)] |
var10_1={"name":"良品铺子","date":"2022-2-4","category":"零食","price":108.0,"desc":"良品铺子经典坚果礼盒8袋装\/1310g食食有爱每日坚果干果休闲零食坚果炒货零食大礼包节日送礼夏威夷果核桃"} var10_matchNr=1 |
含有date属性的food数据 |
$..food[?(@.price<100)] |
var11_1={"name":"黄飞鸿花生","category":"零食","price":28.8,"desc":"黄飞红麻辣花生210g*2袋每日坚果炒货休闲网红囤货零食小吃下酒菜花生米"} var11_2={"name":"百草味","category":"零食","price":34.9,"desc":"百草味休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点原味肉松饼1000g\/箱"} var11_matchNr=2 |
价格小于100的food数据 |
$..food[?(@.price<=$['expensive'])] |
var12_1={"name":"黄飞鸿花生","category":"零食","price":28.8,"desc":"黄飞红麻辣花生210g*2袋每日坚果炒货休闲网红囤货零食小吃下酒菜花生米"} var12_2={"name":"百草味","category":"零食","price":34.9,"desc":"百草味休闲零食小吃整箱蛋糕办公室早餐手撕面包点心传统糕点原味肉松饼1000g\/箱"} var12_matchNr=2 |
价格小于expensive 的food数据 |
6)建立JSON JMSPath提取器,如图31所示。
图31 JSON JMSPath提取器
由于JSON JMSPath提取器一次只能输入一个JSON JMSPath表达式,所以我们只能一建立多个提取器。在树状图“JSON JMESPath Extractor1”右击鼠标,在弹出菜单中选择“复写”,使用相同的方法建立12个JSON JMSPath提取器;名称分别为:JSON JMESPath Extractor2、JSON JMESPath Extractor3hellip;JSON JMESPath Extractor12。变量分别为:v1、v2hellip;v12;表达式分别为:store.food[].name、store.、store.[book.price,food[].price]、store.food [2]、store.food [-2]、store.food [:2]、store.food [1:2]、store.food [-2:]、store.food [?(@.date)]、length(store.food [*])、max_by(store.food, &price).name和min_by(store.food, &price)。