设为首页 加入收藏

TOP

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)
2019-04-01 12:09:32 】 浏览:40
Tags:python3 json 数据 格式 转换 dumps/loads 使用 dict str/str 字符串 字典 相互

在写网络爬虫的时候,有时候会抓取到一些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()

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇从0开始的Python学习013编写一个P.. 下一篇numpy(一)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目