load 和 loads 都是实现“反序列化”,load 通过 json.load(open('*.json')) 这样的格式,从文件句柄中打开文件,加载到Python的变量中,并以字典的格式转换。而 loads 必须对于 Python内存中的序列化对象转换成字符串。
load 和 loads 都是实现 “反序列化”,区别在于(以Python为例):
- loads 针对 内存对象,即将 Python内置数据序列化为字串。如使用json.dumps序列化的对象d_json=json.dumps({'a':1, 'b':2}),在这里 d_json 是一个字串'{"b": 2, "a": 1}',d=json.loads(d_json) #{ b": 2, "a": 1},使用load重新反序列化为dict
- load 针对 文件句柄。如本地有一个 json 文件 a.json 则可以 d=json.load(open('a.json')),相应的,dump 就是将内置类型序列化为 json 对象后写入文件
测试 ( 使用 loads )
list_1 = '[1,2,3,4]'
dict_1 = '{"k1":"v1"}'
print(type(list_1)) # <class 'str'>
print(type(dict_1)) # <class 'str'>
ret = json.loads(list_1)
print(ret, type(ret))
ret = json.loads(dict_1)
print(ret, type(ret))
# 输出
# [1, 2, 3, 4] <class 'list'>
# {'k1': 'v1'} <class 'dict'>
测试( 使用 load ):
# -*- coding: utf-8 -*-
# @Time :
# @Author :
# @Email :
# @File : parse_cinema_info.py
# @Software: PyCharm
import json
def parse_cinema_info():
cinema_info_dict = json.load(open('f:/bj_cinema_all_info.json', "rb"))
all_cinemas = cinema_info_dict['cinemas']
cinemas_list = list()
for cinema in all_cinemas:
d = dict(
cinema_id=cinema['id'],
cinema_name=cinema['nm'],
city='北京',
address=cinema['addr'],
telephone=None
)
cinemas_list.append(d)
with open('f:/bj_cinema.json', 'w') as f:
json.dump(cinemas_list,f, ensure_ascii=False, indent=4)
pass
if __name__ == '__main__':
parse_cinema_info()
pass
使用 dump:
# -*- coding: utf-8 -*-
# @Time :
# @Author :
# @Email :
# @File : parse_cinema_hall_info.py
# @Software: PyCharm
import re
import json
def parse_file():
cinema_hall_info = dict()
with open('f:/pipelines.log', 'rb') as f:
all_lines = f.readlines()
for line in all_lines:
line_str = line.decode('utf-8')
base_time_info = re.findall("\('.*?'\)", line_str)
if base_time_info:
hall_info_list = list()
for base_time in base_time_info:
t = tuple(eval(base_time))
cinema_id, hall_info = t[1], t[8]
if hall_info not in hall_info_list:
hall_info_list.append(hall_info)
cinema_hall_info[cinema_id] = hall_info_list
else:
# print(json.dumps(cinema_hall_info, ensure_ascii=False))
hall_info_list = list()
for cinema in cinema_hall_info:
# print('cinema_id : {0} hall_info : {1}'.format(cinema, cinema_hall_info[cinema]))
d = dict(
cinema_id=cinema,
hall_info=cinema_hall_info[cinema],
seat_count=None
)
hall_info_list.append(d)
else:
with open('f:/bj_cinema_hall.json', 'w') as f:
json.dump(hall_info_list, f, ensure_ascii=False, indent=4)
if __name__ == '__main__':
parse_file()
pass
一个测试 json 文件 (bj_cinema_all_info.json):
{
"cinemas": [
{
"id": 264,
"mark": 0,
"nm": "万达国际影城(CBD店)",
"sellPrice": "48.5",
"addr": "朝阳区建国路93号万达广场B座3层",
"distance": "600m",
"tag": {
"allowRefund": 0,
"buyout": 0,
"cityCardTag": 0,
"deal": 0,
"endorse": 0,
"hallType": [
"IMAX厅",
"RealD 6FL厅",
"4DX厅"
],
"hallTypeVOList": [
{
"name": "IMAX厅",
"url": ""
},
{
"name": "RealD 6FL厅",
"url": ""
},
{
"name": "4DX厅",
"url": ""
}
],
"sell": 1,
"snack": 1,
"vipTag": "折扣卡"
},
"promotion": {
}
},
{
"id": 23,
"mark": 0,
"nm": "百丽宫影城(国贸店)",
"sellPrice": "29",
"addr": "朝阳区建国门外大街1号国贸商城北区B1层B120",
"distance": "1km",
"tag": {
"allowRefund": 0,
"buyout": 0,
"cityCardTag": 0,
"deal": 0,
"endorse": 0,
"hallType": [
"RealD厅"
],
"hallTypeVOList": [
{
"name": "RealD厅",
"url": ""
}
],
"sell": 1,
"snack": 1
},
"promotion": {
}
}
],
"ct_pois": [
{
"ct_poi": "936879945111165696_a15272_c223",
"poiid": 99082156
},
{
"ct_poi": "936879945111165696_a2378_c224",
"poiid": 1541434
},
{
"ct_poi": "936879945111165696_a15748_c225",
"poiid": 94728699
}
],
"paging": {
"hasMore": false,
"limit": 10000,
"offset": 0,
"total": 226
}
}