python的json速记

jshort的Python项目详细描述


jshort

python是json的简写。灵感来自q

这个包允许用很少的代码从json加载、转储到文件。

安装

pip install jshort

用法

# This is an instanceimportj# Loads json from file, stores it in data propertyh=j(input_path='/tmp/data.json')# Get datah.data#{#   "foo": "bar"#}# Display colored contentprint(h)# orh.prt()# {#     "oh": "dayum!"# }# The same, in short# For dataj(i='/tmp/data.json').d# Printj(i='/tmp/data.json').prt()# Write jsonj(output_path='/tmp/out.json',data={"foo":"bar"})# Also write data in a shorter way and change print content setting indentation to 2j(o='/tmp/out.json',d={"hey":"jude"},indent=2).prt()# {#   "hey": "jude"# }# Traversing json documents using https://pypi.org/project/jsonpath-ng/j(d=my_dict).path('key')# Shorter and with more filteringj(d=my_dict).p('sub.*')# Or fintering on an array and from a filej(i='/tmp/data.json').p('key[1]')

有关json遍历的更多信息,请查看https://pypi.org/project/jsonpath-ng/

如果筛选只返回一个结果,那么path方法只返回结果,否则它是一个结果列表。

json作为对象

可以将j实例中的json操作为python对象。尝试以下操作:

o=j(d={'a':{'b':1}}).obj#raw datao.data_#{'a': {'b': 1}}#pretty print itprint(o)#dumps with syntax coloration the json contento.c#Noneo.a#{'b': 1}o.a.b#1o.a['test']=1print(o.d_)#{'a': {'b': 1}, 'test': 1}

享受吧。

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

推荐PyPI第三方库


热门话题
java LineNumberReader。如果查询行为不正确,则返回readLine()   java包含了一个使用AndroidX的工具栏,这让我的应用程序崩溃了   JVM设置通过“java jar”运行应用程序的最佳实践   java如何获取ImageButton宽度   java Oracle SQLLDR实用程序无响应   列出Java获取对象的arrayList中最常见的元素   java使用带有FlowLayout的getContentpane对布局应用更改,但不起作用为什么?   在java中,我可以在画布上绘制画布吗?   编译游戏代码时发生java异常错误   从firestore获取java Webview失败   java将TableLayout中单元格的内容向右对齐   java无法在发布模式下启动活动(使用proguard安卓optimize配置)   java允许在线程期间进行GUI更新。睡觉   java如何对以变量为列表的列表进行排序   API URL上的java Google云端点异常