python解析多层嵌套json

最常用的使用场景:python实现接口测试中,从reponse中取值的操作,需要从多层路径下取到某些值然后作为返回参数
代码如下;

#!/usr/bin/env python
# coding = UTF-8
#Author:Lucky,time:2020/8/19
import jsonpath
# 示例字典
D = {
    'name': 'Ruo Data',
    'web': {
        'site': 'https://www.ruodata.com',
        'basic': 'github',
        "test": [
                "test_syw":"hahha"
                "test_syw":"hahha44"
    'major': 'python'
s = jsonpath.jsonpath(D,"$.web.test[1].test_syw")   #第一种写法(推荐),类似jmeter中的json Extractor的取值样式
s1 = jsonpath.jsonpath(D,"$..test_syw")  #第二种写法,取值前有几层,则写几个..
print (s)   #返回的是一个列表
print(s1)   #返回的是一个列表
print ("".join(s))   #列表转为字符串
        SageCat