使用路径表达式从json对象获取值

json-get的Python项目详细描述


https://img.shields.io/pypi/l/json-get.svghttps://img.shields.io/github/release/srittau/python-json-get/all.svghttps://img.shields.io/pypi/v/json-get.svghttps://travis-ci.org/srittau/python-json-get.svg?branch=master

使用路径表达式从json对象获取值。可选类型 可以检查:

>>> from jsonget import json_get, json_get_default, JList
>>> j = {
...     "foo": {"num": 3.4, "s": "Text"},
...     "arr": [10, 20, 30],
... }
>>> json_get(j, "/foo/num")
3.4
>>> json_get(j, "/arr[1]")
20
>>> json_get(j, "/foo/unknown")
Traceback (most recent call last):
    ...
ValueError: JSON path '/foo/unknown' not found

可以根据以下类型之一检查值: strintfloatboollistdict。 不支持检查空值:

>>> json_get(j, "/foo/num", str)
Traceback (most recent call last):
    ...
TypeError: wrong JSON type str != float

float将匹配任何数字,int将只匹配没有 小数部分:

>>> json_get(j, "/foo/num", float)
3.4
>>> json_get(j, "/foo/num", int)
Traceback (most recent call last):
    ...
TypeError: wrong JSON type int != float

此外,还可以检查列表值的类型:

>>> json_get(j, "/arr", JList(int))
[10, 20, 30]

json_get_default()可用于返回默认值,如果给定 路径不存在:

>>> json_get_default(j, "/bar", "default value")
'default value'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我想访问同一类的不同函数中的函数变量   创建字段/值哈希映射的java反射   java Velocity工具向后兼容性?   cmd JAVA运行时错误:发生JNI错误   java spring引导示例hateoas不工作   java为什么Android Studio和gradle可以使用不同的类路径进行单元测试?   java Selenium Firefox驱动程序初始化   java如何在jar中重新定位gradle依赖项的包   java为什么我的单线程hello world应用程序使用22个OS线程?   正则表达式替换Java字符串中的特殊字符   在任何派生类的构造函数之后运行方法的java   java从输入字符串中找出日期   带有libGdx(Java)音乐的安卓studio可以在Android模拟器上工作,但不能在桌面上工作   java我在getconnection的参数方面有错误吗?   java使用JFileChooser访问选定文件   java如何将Json解析为另一个活动   java使用Resources for html和rest server   java更新列表,列表内容来自循环中的方法   java如何在GLSL中读取完整范围的32位整数纹理