-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_demo.py
More file actions
44 lines (29 loc) · 916 Bytes
/
Copy pathjson_demo.py
File metadata and controls
44 lines (29 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python3
# coding=UTF-8
#================================================================
# Copyright (C) 2018 HALO Ltd. All rights reserved.
#
# 文件名称:json_demo.py
# 创 建 者:Zhangdunfeng
# 创建日期:2018-11-23 14:41:50
# 描 述:解析Json
#
#================================================================
import json
data1 = {
'no':1,
'name':'HALO',
'url':'http://halo.cn'
}
#将字典对象变成json数据格式
json_str=json.dumps(data1)
print("Python原始数据:", repr(data1))
print("JSON对象:", json_str)
with open('data.json','w') as f:
json.dump(json_str,f)
#将JSON对象转换为字典
data2=""
with open('data.json','r') as f:
data2=json.loads(json.load(f))#python从文件读取.json文件是获得字符串,还需转换成python对象
print("data['name']:",data2['name'])
print("data['url']:",data2['url'])