JSON (JavaScript Object Notation),由 RFC 7159 (它取代了 RFC 4627) 和 ECMA-404 指定,是一个受 JavaScript 的对象字面值句法启发的轻量级数据交换格式。JSON独立于编程语言的文本格式来存储和表示数据,现在大部分的数据传输基本使用的都是json格式,经常要从json里面拿到自己想要的数据也就是解析json,python提供了工具帮助我们解析json。
json就是一串表达数据的字符串,字符串有一定的语法规则约束:
- {
- "name": "Kamishiro Rize",
- "age": 22,
- "occupation": "firefighter",
- "traits": [
- "Eagle Eyed",
- "Fast Healer",
- "High Thirst",
- "Hearty Appetite"
- ]
- }
JSON 与 Python 的数据结构和对应关系如下:
| JSON | Python |
|---|---|
| object | dict |
| array | list,tuple |
| string | str,unicode |
| number (int) | int, long |
| number (real) | float |
| true | True |
| false | False |
| null | None |
json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
将字符串解析为python对象
参数说明:
'-Infinity' , 'Infinity' , 'NaN' 。如果遇到无效的 JSON 数字则可以使用它引发异常。- import json
-
- jsonstr = '{ \
- "name": "Kamishiro Rize",\
- "age": 22,\
- "occupation": "firefighter",\
- "traits": [\
- "Eagle Eyed",\
- "Fast Healer",\
- "High Thirst",\
- "Hearty Appetite"\
- ]\
- }'
-
- # print(jsonstr)
- jo = json.loads(jsonstr)
- print(type(jo))
- print(jo)
-
- ‘’'
dict'> - {'name': 'Kamishiro Rize', 'age': 22, 'occupation': 'firefighter', 'traits': ['Eagle Eyed', 'Fast Healer', 'High Thirst', 'Hearty Appetite']}
- ‘''
上面的例子可以看出,json字符串被解析为一个dict的对象。具体json是如何解析的,是由JSONDecoder来实现的。
class json.JSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
解码器的参数和解析函数的参数大部分相同,相同的部分含义也一致。
默认情况下,解码执行以下翻译:
| JSON | Python |
|---|---|
| object -- 对象 | dict |
| array | list |
| string | str |
| number (int) | int |
| number (real) | float |
| true | True |
| false | False |
| null | None |
它还将“NaN”、“Infinity”和“-Infinity”理解为它们对应的“float”值,这超出了JSON规范。
主要的方法有:
decode(s)
解析s,如果给定的 JSON 文档无效则将引发 JSONDecodeError。
raw_decode(s)
从 s 中解码出 JSON 文档(以 JSON 文档开头的一个 str 对象)并返回一个 Python 表示形式为 2 元组以及指明该文档在 s 中结束位置的序号。
这可以用于从一个字符串解码JSON文档,该字符串的末尾可能有无关的数据。
json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
对json文件fp进行解析(fp对象要支持read()方法),其他参数与loads完全一致。
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
将一个python对象生成json的str对象。
参数说明:
(item_separator, key_separator) 元组。当 indent 为 None 时,默认值取 (', ', ': '),否则取 (',', ': ')。为了得到最紧凑的 JSON 表达式,你应该指定其为 (',', ':') 以消除空白字符。- import json
-
- # python object(dictionary) to be dumped
- dict1 ={
- "emp1":{
- "name":"Lisa",
- "designation":"programmer",
- "age":34,
- "salary":54000.01
- },
- "emp2":{
- "name":"Elis",
- "designation":"Trainee",
- "age":24,
- "salary":40000.00
- },
- }
- jdict1 = json.dumps(dict1)
- print(jdict1)
- jdict1 = json.dumps(dict1, indent = 6)
- print(jdict1)
-
- ‘’'
- {"emp1": {"name": "Lisa", "designation": "programmer", "age": 34, "salary": 54000.01}, "emp2": {"name": "Elis", "designation": "Trainee", "age": 24, "salary": 40000.0}}
- {
- "emp1": {
- "name": "Lisa",
- "designation": "programmer",
- "age": 34,
- "salary": 54000.01
- },
- "emp2": {
- "name": "Elis",
- "designation": "Trainee",
- "age": 24,
- "salary": 40000.0
- }
- }
- ‘''
class json.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
用于Python数据结构的可扩展JSON编码器。
默认支持以下对象和类型:
| Python | JSON |
|---|---|
| dict | object -- 对象 |
| list, tuple | array |
| str | string |
| int, float, int 和 float 派生的枚举 | 数字 |
| True | true |
| False | false |
| None | null |
除了上面这些类型,JSONEncoder不支持自定义的类型,需要自己写基于上面这个编码器的子类。
主要方法:
default(o)
在子类中实现这种方法使其返回 o 的可序列化对象,或者调用基础实现(引发 TypeError )。
encode(o)
返回 Python o 数据结构的 JSON 字符串表达方式。
iterencode(o)
编码给定对象 o ,并且让每个可用的字符串表达方式。
- import json
-
- class Base:
- def __init__(self, id, desc):
- self.id = id
- self.desc = desc
-
- def foo(self):
- print(f'Base {self.id=}, {self.desc=}')
-
- IDSTART = 1100001
-
- class Teacher(Base):
- def __init__(self, name, course):
- self.name = name
- self.course = course
-
- class Student(Base):
- def __init__(self, name, age):
- self.name = name
- self.age = age
- self.score = {'Chinese':90, 'Math':95, 'English':90}
- self.tearchers = [Teacher('Chen', 'Chinese'), Teacher('Wang', 'Math'), Teacher('Su', 'English')]
- global IDSTART
- IDSTART += 1
- super().__init__(IDSTART, 'Student ' + name)
-
- def foo(self):
- print(f'Student {self.name=}, {self.age=}')
-
-
- class MyEncoder(json.JSONEncoder):
- def default(self, obj):
- if type(obj) not in (dict, list, tuple, str, int, float, True, False, None):
- return str(vars(obj))
-
- return json.JSONEncoder.default(self, obj)
-
-
- b = Base(10011, 'Base Object')
- b.foo()
- jb = json.dumps(b, cls=MyEncoder)
- print(jb)
-
- s = Student('John', 12)
- s.foo()
- js = json.dumps(s, cls=MyEncoder)
- print(js)
-
- ’’’
- Base self.id=10011, self.desc='Base Object'
- "{'id': 10011, 'desc': 'Base Object'}"
- Student self.name='John', self.age=12
- "{'name': 'John', 'age': 12, 'score': {'Chinese': 90, 'Math': 95, 'English': 90}, 'tearchers': [<__main__.Teacher object at 0x10fede450>, <__main__.Teacher object at 0x10fe05a50>, <__main__.Teacher object at 0x10fede510>], 'id': 1100002, 'desc': 'Student John'}"
- ‘’‘
json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
将python对象以json格式写入文件fp(支持write()方法)
json 模块始终产生 str 对象而非 bytes 对象。因此,fp.write() 必须支持 str 输入。