?
json.load讀取文件(file),解析為json對象(dict類型);
json.loads讀取字符串(string),解析為json對象;
json.dump寫文件(file),將json對象寫入文件;
json.dumps寫字符串,將json對象寫成string。
import json
import os
json_obj = {
'name':'Michael',
'age':13
}
print("json dict:",type(json_obj)) # json對象是一個dict類型
json_str = json.dumps(json_obj) # 將json dict寫成str
print("json.dumps:",type(json_str))
json_obj_2 = json.loads(json_str) # 將json str解析成json dict
print("json.loads:",type(json_obj_2))
json_file = open("json_file.json",'w+')
json.dump(json_obj, json_file) # 將json dict寫入文件
print("json.dump:",type(json_file))
json_file.close()
json_file = open("json_file.json",'r') # json.load必須以讀方式打開文件,讀寫方式都不行
json_file_obj = json.load(json_file) # 加載json文件,返回值為dict
print("json.load:",type(json_file_obj))
json_file.close()
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
