json

2021-05-03 11:28

阅读:610

标签:open   包含   import   就是   python   类型   数据类型   int   print   

import json

lt = [
    {name: 王宝强, age: 30},
    {name: 贾乃亮, age: 36},
    {name: 马蓉蓉, age: 33},
    {name: 宋吉吉, age: 40},
    {name: 李小璐, age: 43},
]
# json字符串
#          ------loads------->
# 包含json的类文件对象
#                   ------load------->
# 具有read()或者write()方法的对象就是类文件对象
    # f = open(“a.txt”,”r”) f就是类文件对象
string = json.dumps(lt)
print(type(string))  #   # json字符串
print(string)  # [{"name": "\u738b\u5b9d\u5f3a", "age": 30}, {"name": "\u8d3e\u4e43\u4eae", "age": 36}, {"name": "\u9a6c\u84c9\u84c9", "age": 33}, {"name": "\u5b8b\u5409\u5409", "age": 40}, {"name": "\u674e\u5c0f\u7490", "age": 43}]
obj = json.loads(string)

print(type(obj))  # 

json.dump(lt, open(json.txt, w, encoding=utf8))
obj = json.load(open(json.txt, r, encoding=utf8))
print(type(obj))  # 
print(obj)  # [{‘name‘: ‘王宝强‘, ‘age‘: 30}, {‘name‘: ‘贾乃亮‘, ‘age‘: 36}, {‘name‘: ‘马蓉蓉‘, ‘age‘: 33}, {‘name‘: ‘宋吉吉‘, ‘age‘: 40}, {‘name‘: ‘李小璐‘, ‘age‘: 43}]


              

json

标签:open   包含   import   就是   python   类型   数据类型   int   print   

原文地址:https://www.cnblogs.com/yzg-14/p/12121863.html


评论


亲,登录后才可以留言!