当前位置: 移动技术网 > IT编程>脚本编程>Python > python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

2019年04月02日  | 移动技术网IT编程  | 我要评论

使徒行者粤语12,令政策出逃,石榴石的功效与作用

在写网络爬虫的时候,有时候会抓取到一些json格式的字符串,想要通过python字典的方式对字串中的内容进行寻址,则需要将json字符串先转换为python字典。

dumps()函数: 

loads()函数:

示例:

import json

class fordatas:
    def __init__(self):
        pass
    
    def testjson(self):
        # 定义一个字典
        d = {'a': 1,
             'b': 2,
             'c': 'asdf'}
        print('d:', d, type(d))

        # dict to str
        d1 = json.dumps(d)
        print('d1:', d1, type(d1))

        # str to dict
        d2 = json.loads(d1)
        print('d2:', d2, type(d2))

if __name__ == '__main__':
    tt = fordatas()
    tt.testjson()

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网