栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【猫狗日记】 jsonpath的简单应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【猫狗日记】 jsonpath的简单应用

"""

jsonpath
jsonpath 使用场景
1.接口数据的提取,用于其他的接口
2.只能处理json格式的数据 (python中叫字典,java叫json)

jsonpath 使用
1.导包
2.特殊字符表示特殊的含义
    $ :表示根元素
    . [] 表示子元素
    .. 表示递归  递归查找   【掌握】
    []  : 子元素操作符, 切片取值 索引取值  字段名称 取值(多个字段用逗号隔开)
    ?(@): :jsonpath(teacher_info, "$..python.[?(@.age<20)]")  过滤表达式
  ?(@): sonpath(teacher_info, "$..python.[?(@.age<38 && @.height>175)]")   多条件过滤




"""
import pprint

from jsonpath import jsonpath

teacher_info = {"lemon":
    {"python": [
        {"name": "海励",
         "sex": "男",
         "age": 30,
         "height": 175,
         "info": "python自动化老师"
         },
        {"name": "木森",
         "sex": "男",
         "age": 28,
         "height": 185,
         "info": "python测开老师"
         },
        {"name": "小简",
         "sex": "女",
         "age": 18,
         "height": 170,
         "info": "python自动化老师"
         },
        {"name": "心蓝",
         "sex": "男",
         "age": 28,
         "height": 185,
         "info": "python测开老师"
         },
        {"name": "雨泽",
         "sex": "男",
         "age": 28,
         "height": 190,
         "info": "python自动化老师"
         }
    ],
        "java": {
            "name": "三宝",
            "sex": "男",
            "age": 30,
            "height": 185.5,
            "info": "java测开老师"
        }
    }
}
res1 = jsonpath(teacher_info, "$.lemon.*") #取出lemon下的所有内容
res2 = jsonpath(teacher_info, "$.lemon.[0]")  # 索引取,取出第一个字典内容

res3 = jsonpath(teacher_info, "$.lemon.[0,1]") # 切片取值,注意这个区间是闭区间。
res4 = jsonpath(teacher_info, "$.lemon.[0:5:2]") #切片,带步长
res5= jsonpath(teacher_info, "$.lemon.[name,age,height]")  #获取指定的字段 【】指定
res6 = jsonpath(teacher_info, "$..python.[?(@.age<38 && @.height>175)]") #按照查询条件,@@and
res7 = jsonpath(teacher_info, "$..python.[?(@.age<38 || @.height>175)]") #按照查询条件|| 或
res8 = jsonpath(teacher_info, "$..python.[?(@.name in ['小简','木森'])].[age,info]") # 输出指定Key的,指定value
res9 = jsonpath(teacher_info, "$..python.[?(@.name in ['小简','木森'])]")  #输出指定key的 全部信息
pprint.pprint(res1)  #格式化输出
pprint.pprint(res2)
pprint.pprint(res3)
pprint.pprint(res4)
pprint.pprint(res5)
pprint.pprint(res6)
pprint.pprint(res7)
pprint.pprint(res8)
pprint.pprint(res9)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号