1 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("https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?os=ios&for_mobile=1&start=0&count=18&loc_id=108288&_=0") 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 ) )