1 #/usr/bin/env python3 2 import json 3 print("part1: str -> json") 4 str1="[1,2,3]" 5 jl=json.loads(str1) # list 6 print(type(jl)) 7 jd=json.dumps(str1) # str 8 print(type(jd)) 9 10 11 print(" part2: list -> json") 12 d=[1,2,3,4,5] 13 a=json.dumps(d) # str 14 print(type(a)) 15 b=json.loads(a) # list 16 print(type(b)) 17 18 19 print(" part3: 文件保存") 20 with open('json.txt','w') as f: 21 json.dump(d,f) 22 # f.write(json.dumps(d)) 23 24 with open('json.txt','r') as f: 25 a=json.load(f) # list 26 print(type(a)) 27 28 29 with open('json.txt', 'r') as f: 30 aa=f.read() 31 bb=json.loads(aa) # list 32 print(type(bb))