当前位置:首页 > Python教程 > python技巧

python json与字典对象互相转换

 

            import requests
import json

            ‘‘‘

            json.loads(json_str) json字符串转换成字典
json.dumps(dict) 字典转换成json字符串 


            ‘‘‘

            # 这是一个ajax发起的get请求,获取一个json对象
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")
json_response = r.content.decode()  # 获取r的文本 就是一个json字符串

# 将json字符串转换成dic字典对象
dict_json = json.loads(json_response)
print(type(dict_json))

# 将字典转换成json字符串
str_json = json.dumps( dict_json )
print(type(str_json))

# 字典转换成json 存入本地文件
with open(./a.txt,w) as f:
    # 设置不转换成ascii  json字符串首缩进
    f.write( json.dumps( dict_json,ensure_ascii=False,indent=2 ) )

 

 

参考:

https://www.cnblogs.com/niuu/p/10106897.html

https://www.cnblogs.com/Lin-Yi/p/7640147.html

原文:https://www.cnblogs.com/sea-stream/p/11204595.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐