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 str
、float
、bool
或{
类型检查
类型
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语法,因此您可能会得到SIGSEGV
或corrupt JSON的无限循环。同样地,在一些损坏的json的情况下,它也能正常工作。在
性能
在examples/
文件夹中有一些示例json和阅读示例来分析性能。在
C库
可以使用Jsonic JSON阅读器库来实现C/C++。在
许可证
麻省理工学院
- 项目
标签: