Jmespath 的使用
{"data": [{"last_reply_at": "2021-10-07T05:40:14.376Z","visit_count": 1,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "zhangdandan"},"id": "615e883df7759804f43dd3df","author_id": "6153d8beabc8d9ccc9ca36c9","title": "重阳节到了","reply_count": 1,"create_at": "2021-10-07T05:40:13.558Z","good": false,"content": "<div class=\"markdown-text\"><p>是不是又可以放假了,哈哈哈哈哈哈哈哈哈哈哈哈<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:40:09.496Z","visit_count": 1,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "zhangdandan"},"id": "615e8839f7759804f43dd3db","author_id": "6153d8beabc8d9ccc9ca36c9","title": "重阳节到了","reply_count": 1,"create_at": "2021-10-07T05:40:09.380Z","good": false,"content": "<div class=\"markdown-text\"><p>是不是又可以放假了,哈哈哈哈哈哈哈哈哈哈哈哈<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:40:01.509Z","visit_count": 1,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "a592518"},"id": "615e8831f7759804f43dd3d7","author_id": "6153d783abc8d9ccc9ca368e","title": "fgdgdgdfgfdg","reply_count": 1,"create_at": "2021-10-07T05:40:01.396Z","good": false,"content": "<div class=\"markdown-text\"><p>表示的是从服务器中返回的 对应 topic_id 字段的值。<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:39:48.512Z","visit_count": 1,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "test2"},"id": "615e8824f7759804f43dd3d3","author_id": "60e04cade14a422006d945f5","title": "这么近,那么远","reply_count": 1,"create_at": "2021-10-07T05:39:48.398Z","good": false,"content": "<div class=\"markdown-text\"><p>啦啦啦啦啦啦<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:39:48.380Z","visit_count": 0,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "test2"},"id": "615e8824f7759804f43dd3d2","author_id": "60e04cade14a422006d945f5","title": "666888999","reply_count": 0,"create_at": "2021-10-07T05:39:48.380Z","good": false,"content": "<div class=\"markdown-text\"><p>0123456789<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:39:40.624Z","visit_count": 1,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "test2"},"id": "615e881cf7759804f43dd3ce","author_id": "60e04cade14a422006d945f5","title": "这么近,那么远","reply_count": 1,"create_at": "2021-10-07T05:39:40.408Z","good": false,"content": "<div class=\"markdown-text\"><p>啦啦啦啦啦啦<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:39:40.392Z","visit_count": 0,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "test2"},"id": "615e881cf7759804f43dd3cd","author_id": "60e04cade14a422006d945f5","title": "666888999","reply_count": 0,"create_at": "2021-10-07T05:39:40.392Z","good": false,"content": "<div class=\"markdown-text\"><p>0123456789<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:39:26.420Z","visit_count": 1,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "zhanglanling"},"id": "615e583ff7759804f43dc46a","author_id": "61540250f7759804f43d4974","title": "321321321","reply_count": 16,"create_at": "2021-10-07T02:15:27.389Z","good": false,"content": "<div class=\"markdown-text\"><p>098765432100<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:39:25.369Z","visit_count": 1,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "abtester"},"id": "615e880df7759804f43dd3c9","author_id": "6153dd8df7759804f43d3fd2","title": "好家伙多过所晕过多所多付付付付","reply_count": 0,"create_at": "2021-10-07T05:39:25.369Z","good": false,"content": "<div class=\"markdown-text\"><p>广告设计所军军军军军军<\/p>\n<\/div>"},{"last_reply_at": "2021-10-07T05:39:24.698Z","visit_count": 0,"tab": "ask","top": false,"author": {"avatar_url": "\/public\/images\/userlogo.png","loginname": "guiying"},"id": "615e880cf7759804f43dd3c8","author_id": "6153d726abc8d9ccc9ca3687","title": "我又来酷酷酷酷酷酷酷酷酷酷","reply_count": 0,"create_at": "2021-10-07T05:39:24.698Z","good": false,"content": "<div class=\"markdown-text\"><p>手术室的回归方法范冰冰和经济<\/p>\n<\/div>"}],"success": true}
上面是服务器返回的结果。在这个结果提取变量。
比如要提取 reply_count值大于 5 的所有话题的id。
使用原来的提取方式已经不再适用,因为返回的结果中数据位置已经不再是确定的,有可能第一条数据符合上述要求,也有可能第二条数据符合要求。
| 符号 | 说明 |
|---|---|
| == | 等于 |
| > | 大于 |
| >= | 大于等于 |
| < | 小于 |
| <= | 小于等于 |
| != | 不等于 |
这就需要我们使用灵活的提取方式了。
- 查找结果中回复数大于5的话题id。
data[?reply_count > `5`].id
- ? 表示查询条件
表示大于
5表示数字5 这里需要注意⚠️: 当条件中有数字或者bool类型的值(true,false) 需要添加 ``
查询 top值为 false 的话题id
data[?top==`false`].id
查询tab值为 ask 的话题id
data[?tab=='ask'].id
'ask'因为ask的值在 json中是字符串"ask"
总结
jmespath 适用的场景:
- 当数据有很多条,需要根据条件来进行过滤的时候进行查找。
- 也可以直接查找。
可以官网上的例子 进行高效 学习。 https://jmespath.org/tutorial.html
