Python - jsonpath 简单使用
- 第三方包使用的时候需要单独安装
- 使用场景:快速提取接口返回的JSON串中的某一个字段的值
import json
import jsonpath
json_str = '''
{
"success": true,
"code": 200,
"message": null,
"data": {
"message": null,
"result": null,
"pageNum": null,
"pageSize": null,
"totalRows": null,
"totalPages": null,
"recommendList": [
{
"recommendKeyWord": "西门子 PLC S7-1200系列 CPU模块",
"recommendHighLight": "西门子 PL<em>C</em> <em>S</em>7<em>-</em><em>1</em><em>2</em><em>0</em><em>0</em>系列 <em>C</em>P<em>U</em>模块",
"productCodeAccessId": "24ff7c58b96449ad90ffd2dbb5b1be44",
"productCode": null,
"productSerialAccessId": "68fd719a771f4fec8ef0e1ebe594bf11",
"productSerialCode": "ILC-S7-1200-CPU",
"childCategoryCode": "I10",
"parentCategoryCode": "I",
"parentCategoryAccessId": "24f420a6f5d2454780d51628b2a7c808",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "开关电源 导轨式安装 欧姆龙 S8VK-C系列 60/120/240/480 单组输出",
"recommendHighLight": "开关电源 导轨式安装 欧姆龙 <em>S</em>8VK<em>-</em><em>C</em>系列 <em>6</em><em>0</em>/<em>1</em><em>2</em><em>0</em>/<em>2</em><em>4</em><em>0</em>/<em>4</em>8<em>0</em> 单组输出",
"productCodeAccessId": "6f25a5fe6a914af9bbcb4e1e3f531588",
"productCode": null,
"productSerialAccessId": "823107ded448415eb96f658ebfabb3d3",
"productSerialCode": "IKD-S8VKC",
"childCategoryCode": "I04",
"parentCategoryCode": "I",
"parentCategoryAccessId": "24f420a6f5d2454780d51628b2a7c808",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "摩莎 网关 1100Mbps 1端口 RS232/422/485",
"recommendHighLight": "摩莎 网关 <em>1</em><em>1</em><em>0</em><em>0</em>Mbp<em>s</em> <em>1</em>端口 R<em>S</em><em>2</em>3<em>2</em>/<em>4</em><em>2</em><em>2</em>/<em>4</em>85",
"productCodeAccessId": "776832d8fbb44061bc381c70c0ff1a61",
"productCode": null,
"productSerialAccessId": "b4d018121472452f82028a161dd09f89",
"productSerialCode": "IVX-13",
"childCategoryCode": "I18",
"parentCategoryCode": "I",
"parentCategoryAccessId": "24f420a6f5d2454780d51628b2a7c808",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "西门子 PLC S7-1200系列 温度模块",
"recommendHighLight": "西门子 PL<em>C</em> <em>S</em>7<em>-</em><em>1</em><em>2</em><em>0</em><em>0</em>系列 温度模块",
"productCodeAccessId": "24ff7c58b96449ad90ffd2dbb5b1be44",
"productCode": null,
"productSerialAccessId": "b95bc807da9a4c609e3bdf05a54fe984",
"productSerialCode": "ILC-S7-1200-WD",
"childCategoryCode": "I10",
"parentCategoryCode": "I",
"parentCategoryAccessId": "24f420a6f5d2454780d51628b2a7c808",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "西门子 PLC S7-1200系列 模拟量模块",
"recommendHighLight": "西门子 PL<em>C</em> <em>S</em>7<em>-</em><em>1</em><em>2</em><em>0</em><em>0</em>系列 模拟量模块",
"productCodeAccessId": "24ff7c58b96449ad90ffd2dbb5b1be44",
"productCode": null,
"productSerialAccessId": "b1837e9e02364b61980fbac63c2e23f4",
"productSerialCode": "ILC-S7-1200-MNL",
"childCategoryCode": "I10",
"parentCategoryCode": "I",
"parentCategoryAccessId": "24f420a6f5d2454780d51628b2a7c808",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "世达 6孔背绒砂碟Ø125xP80/125xP240/Ø125xP600",
"recommendHighLight": "世达 <em>6</em>孔背绒砂碟Ø<em>1</em><em>2</em>5xP8<em>0</em>/<em>1</em><em>2</em>5xP<em>2</em><em>4</em><em>0</em>/Ø<em>1</em><em>2</em>5xP<em>6</em><em>0</em><em>0</em>",
"productCodeAccessId": "655e38b7d61a4aee9ded85771c853e75",
"productCode": null,
"productSerialAccessId": "92fa0eabb8de4685bc5d17324783aa97",
"productSerialCode": "LSD-556",
"childCategoryCode": "L04",
"parentCategoryCode": "L",
"parentCategoryAccessId": "b865daf29e4b4a4f9176acf0bd7be47f",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "西门子 PLC S7-1200系列 I/O扩展模块",
"recommendHighLight": "西门子 PL<em>C</em> <em>S</em>7<em>-</em><em>1</em><em>2</em><em>0</em><em>0</em>系列 I/O扩展模块",
"productCodeAccessId": "24ff7c58b96449ad90ffd2dbb5b1be44",
"productCode": null,
"productSerialAccessId": "91b299b93d7b42daa6e4a54d340fc2bc",
"productSerialCode": "ILC-S7-1200-IO",
"childCategoryCode": "I10",
"parentCategoryCode": "I",
"parentCategoryAccessId": "24f420a6f5d2454780d51628b2a7c808",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "开关电源 平板式安装 欧姆龙 S8FS-C系列 25/35/75/100/150/200/350 单组",
"recommendHighLight": "开关电源 平板式安装 欧姆龙 <em>S</em>8F<em>S</em><em>-</em><em>C</em>系列 <em>2</em>5/35/75/<em>1</em><em>0</em><em>0</em>/<em>1</em>5<em>0</em>/<em>2</em><em>0</em><em>0</em>/35<em>0</em> 单组",
"productCodeAccessId": "6f25a5fe6a914af9bbcb4e1e3f531588",
"productCode": null,
"productSerialAccessId": "9276f07e98504adeb6fcf74250e8d7cf",
"productSerialCode": "IKD-S8FSC",
"childCategoryCode": "I04",
"parentCategoryCode": "I",
"parentCategoryAccessId": "24f420a6f5d2454780d51628b2a7c808",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "史丹利(Stanley) 23件装公制塞尺0.02-1.00mm",
"recommendHighLight": "史丹利(<em>S</em>tanley) <em>2</em>3件装公制塞尺<em>0</em>.<em>0</em><em>2</em><em>-</em><em>1</em>.<em>0</em><em>0</em>mm",
"productCodeAccessId": "ea85239bb0e343a88b17a66e54b2dca2",
"productCode": null,
"productSerialAccessId": "11cb6b4d78824be5907d7d7ff9d107c2",
"productSerialCode": "LMC-000041",
"childCategoryCode": "L02",
"parentCategoryCode": "L",
"parentCategoryAccessId": "b865daf29e4b4a4f9176acf0bd7be47f",
"recommendType": 0,
"paramInfoList": null
},
{
"recommendKeyWord": "博世 GCO 14-24型材切割机",
"recommendHighLight": "博世 G<em>C</em>O <em>1</em><em>4</em><em>-</em><em>2</em><em>4</em>型材切割机",
"productCodeAccessId": "30210f70152c481da73ff521f0ba0937",
"productCode": null,
"productSerialAccessId": "de6f13b0ddba4c93972109ad42ec99cf",
"productSerialCode": "LDC-BS-000081",
"childCategoryCode": "L09",
"parentCategoryCode": "L",
"parentCategoryAccessId": "b865daf29e4b4a4f9176acf0bd7be47f",
"recommendType": 0,
"paramInfoList": null
}
]
}
}
'''
json_data = json.loads(json_str)
print(jsonpath.jsonpath(json_data, '$.data.recommendList[*].productSerialCode'))
# out:
'''
['ILC-S7-1200-CPU', 'IKD-S8VKC', 'IVX-13', 'ILC-S7-1200-WD', 'ILC-S7-1200-MNL', 'LSD-556', 'ILC-S7-1200-IO', 'IKD-S8FSC', 'LMC-000041', 'LDC-BS-000081']
'''
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/16807974.html