python 字典轉json並儲存檔案

2021-09-28 21:08:20 字數 726 閱讀 5732

import requests

2 import json

3 '''

4 json.loads(json_str) json字串轉換成字典

5 json.dumps(dict) 字典轉換成json字串

6 7 '''

8 # 這是乙個ajax發起的get請求,獲取乙個json物件

9 r = requests.get("")

10 json_response = r.content.decode() # 獲取r的文字 就是乙個json字串

11 12 # 將json字串轉換成dic字典物件

13 dict_json = json.loads(json_response)

14 print(type(dict_json))

15 16 # 將字典轉換成json字串

17 str_json = json.dumps( dict_json )

18 print(type(str_json))

19 20 # 字典轉換成json 存入本地檔案

21 with open('./a.txt','w') as f:

22 # 設定不轉換成ascii json字串首縮排

23 f.write( json.dumps( dict_json,ensure_ascii=false,indent=2 ) )

json 轉字典 字典轉json

brief 把格式化的json格式的字串轉換成字典 param jsonstring json格式的字串 return 返回字典 json格式字串轉字典 nsdictionary dictionarywithjsonstring nsstring jsonstring nsdata jsondata...

python 列表 字典轉json

一 dictionary 轉為json 將dict轉為json,這裡利用包json import json aitem aitem id 2203 aitem title title aitem subtitle sub title bitem bitem id 2842 bitem title b...

python 列表 字典轉json

一 dictionary 轉為json 將dict轉為json,這裡利用包json import json aitem aitem id 2203 aitem title title aitem subtitle sub title bitem bitem id 2842 bitem title b...