Jsonic JSON阅读器库的Python绑定。

pyjsonic的Python项目详细描述


python jsonic

JsonicJSON读取器库的Python绑定。在

安装

皮普:

pip install pyjsonic

吉特:

^{pr2}$

使用

导入

importjsonic

类型和功能

函数:来自_文件

读取文件并返回Jsonic()对象。在

root=jsonic.from_file("file.json")

类型:Jsonic

root=jsonic.Jsonic("[1, 2, 3, 4]")
会员:Jsonic.type类型

类型成员可用于检查对象和数组类型。除了对象和数组类型,您将得到常规python strfloatbool或{}对象。在

类型检查
类型

jsonic.TYPE_OBJECT
jsonic.TYPE_ARRAY

Jsonic.type可用于对象或数组。Object和array值返回为Jsonic()个对象。空值返回为Jsonic.Null对象。否则,它将作为常规python类型返回。在

会员:Jsonic.version版

pythonjsonic的版本。在

会员:Jsonic.json文件

JSON字符串。在

类型:Jsonic.Null

JSON空类型。在

方法:root()

如果root.type不是数组或对象,则返回JSON根的值。否则,它将不返回任何值。在

root=jsonic.Jsonic("1234")print(root.root())# 1234root=jsonic.Jsonic("\"foo\"")print(root.root())# fooroot=jsonic.Jsonic("true")print(root.root())# Trueroot=jsonic.Jsonic("null")print(root.root())# jsonic.Nullroot=jsonic.Jsonic("{}")print(root.root())# Noneprint(root.type)# jsonic.TYPE_OBJECTroot=jsonic.Jsonic("[]")print(root.root())# Noneprint(root.type)# jsonic.TYPE_ARRAY
方法:len()

获取数组的长度。在

方法:key(key)

返回键的值。在

方法:项目(索引)

返回数组上的索引项。在

方法:iterItem(index=0)

从上次迭代的项乘以索引迭代数组项。在

root=jsonic.Jsonic("[1, 2, 3, 4]")print(array.iterItem())# 1print(array.iterItem())# 2print(array.iterItem(1))# 4print(array.iterItem())# Nonearray.reset()print(array.iterItem())# 1

方法:iterKey(键)

从上次迭代的对象迭代对象键。在

root=jsonic.Jsonic("{\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}")print(array.iterKey("a"))# 1print(array.iterKey("b"))# 2print(array.iterKey("c"))# 3print(array.iterKey("b"))# Nonearray.reset()print(array.iterKey("b"))# 2

方法:reset()

重置迭代电流。在

示例

读取JSON数据的示例

importjsonicroot=jsonic.from_file("heroes.json")print("Root Type: %d"%root.type)print("Squad: %s"%root.iterKey("squadName"))print("Hometown: %s"%root.iterKey("homeTown"))print("Formed: %d"%root.iterKey("formed"))print("Active: %d"%root.iterKey("active"))members=root.iterKey("members")print("Members: (%d total)"%members.len())whileTrue:member=members.iterItem()ifnotmember:breakname=member.iterKey("name")age=member.iterKey("age")powers=member.iterKey("powers")print("\tName: %s"%name)print("\tAge: %s"%age)print("\tPowers (%d total):"%powers.len())whileTrue:power=powers.iterItem()ifnotpower:breakprint("\t\t%s"%power)print()

示例JSON(英雄.json)公司名称:

{"squadName":"Super hero squad","homeTown":"Metro City","formed":2016,"secretBase":"Super tower","active":true,"members":[{"name":"Molecule Man","age":29,"secretIdentity":"Dan Jukes","powers":["Radiation resistance","Turning tiny","Radiation blast"]},{"name":"Madame Uppercut","age":39,"secretIdentity":"Jane Wilson","powers":["Million tonne punch","Damage resistance","Superhuman reflexes"]},{"name":"Eternal Flame","age":1000000,"secretIdentity":"Unknown","powers":["Immortality","Heat Immunity","Inferno","Teleportation","Interdimensional travel"]}]}

语法检查

这个库不检查JSON语法,因此您可能会得到SIGSEGVcorrupt JSON的无限循环。同样地,在一些损坏的json的情况下,它也能正常工作。在

性能

examples/文件夹中有一些示例json和阅读示例来分析性能。在

C库

可以使用Jsonic JSON阅读器库来实现C/C++。在

许可证

麻省理工学院

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

推荐PyPI第三方库


热门话题
java类。getResource和ClassLoader。getSystemResource:有没有理由选择其中一个而不是另一个?   在Java中以编程方式粘贴后恢复剪贴板   Java字符串到日期没有时间   JavaSpring注释:@Component起作用,@Repository不起作用   java“addScript”在HSQL中是否有最大记录计数?   java如何将值从JDialog框返回到父JFrame?   java我的模块库的用户有没有办法访问尚未导出的类?   java javac:未找到命令   java如何解决jsoup错误:无法找到请求目标的有效证书路径   类中的java作用域变量   Java中集合实现中的arraylist add()方法不起作用   java如何使用while循环和从用户接收输入来近似Pi?   java Spring安全CSRF培训模式   在安卓系统中,如何通过在警报框外单击来限制用户?