最近看到好几篇类似“n行Python代码…”的博文,看起来还挺不错,简洁、实用,传播了知识、带来了阅读量,撩动了老猿的心,决定跟风一把,推一个“n行Python代码系列”文章。
今天介绍使用2行代码将JSON文本转换成Python字典类型的简单快速方法。后续更多“n行Python代码系列”文章请参考免费专栏《https://blog.csdn.net/laoyuanpython/category_10858655.html n行Python代码系列》。
老猿在通过程序获取CSDN博文的评论区信息时,发现其返回信息是一个复杂的JSON格式文本,内容示例如下:
{"code":200,"message":"success","data":
{"count":47,"pageCount":3,"floorCount":27,"list":
[
{"info":
{"commentId":15302301,"articleId":113663811,"parentId":0,"postTime":"2021-03-07 19:21:17","content":"恭喜你","userName":"ria4com","digg":0,"diggArr":[],"parentUserName":null,"parentNickName":null,"avatar":"https://profile.csdnimg.cn/B/2/3/3_ria4com","nickName":"低代码游戏开发","dateFormat":"1 小时前","tag":"","parentTag":null,"years":null,"vip":null,"vipIcon":null,"companyBlog":null,"companyBlogIcon":null,"flag":null,"flagIcon":null,"levelIcon":null},
"sub":[],
"pointCommentId":null
},
{
"info":
{"commentId":14922981,"articleId":113663811,"parentId":0,"postTime":"2021-02-05 15:06:21","content":"大佬无敌强","userName":"weixin_44671737","digg":0,"diggArr":[],"parentUserName":null,"parentNickName":null,"avatar":"https://profile.csdnimg.cn/E/3/5/3_weixin_44671737","nickName":"兴趣使然的程序猿","dateFormat":"1 月前","tag":"爱码士","parentTag":null,"years":null,"vip":null,"vipIcon":null,"companyBlog":null,"companyBlogIcon":null,"flag":null,"flagIcon":null,"levelIcon":null},
"sub":
[{"commentId":14923083,"articleId":113663811,"parentId":14922981,"postTime":"2021-02-05 15:13:57","content":"感谢支持!一起加油!","userName":"LaoYuanPython","digg":0,"diggArr":[],"parentUserName":"weixin_44671737","parentNickName":"兴趣使然的程序猿","avatar":"https://profile.csdnimg.cn/0/8/1/3_laoyuanpython","nickName":"LaoYuanPython","dateFormat":"1 月前","tag":"爱码士","parentTag":"爱码士","years":null,"vip":null,"vipIcon":null,"companyBlog":null,"companyBlogIcon":null,"flag":null,"flagIcon":null,"levelIcon":null}],
"pointCommentId":null
}
]
}
}
可以看到这个数据是比较复杂的,但整体就是一个符合JSON格式要求的文本,相对一个评论来说,管理的数据冗余太多,有些数据老猿也没弄明白含义,只关注了响应码code、响应消息message、评论条数count、评论时间postTime、评论内容content、评论发表者userName、评论者昵称nickName等信息。
针对该响应码需要进行数据分析时,如果将其转换成标准字典类型,则数据访问代码好实现,于是利用了Python的内置JSON模块将上述文本转换成了字典类型,再进行数据解析,发现真的好用。
JSON模块是Python内置模块,无需单独安装,直接import就可以使用,主要用来进行Python对象的序列化和反序列化。该模块中常用的方法有以下四个:
将Python对象序列化为Json格式的数据流并写入文件类型的对象中
将Python对象序列化为Json格式的字符串
从文件类型的对象中读取Json格式的数据并反序列化成Python对象
将包含Json格式数据的字符串反序列化成Python对象
本文主要用到JSON模块的loads函数,用于将字符文本转换为Python内置的标准类型。
loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
*
:命名关键字参数分隔符,关于命名关键字参数请参考《https://blog.csdn.net/LaoYuanPython/article/details/94406075 Python中sorted(iterable, *, key=None, reverse=False)函数参数定义中的独立星号(*)的含义
》的介绍>>> import json
>>> text = """{"code":200,"message":"success","data":
{"count":47,"pageCount":3,"floorCount":27,"list":
[
{"info":
{"commentId":15302301,"articleId":113663811,"parentId":0,"postTime":"2021-03-07 19:21:17","content":"恭喜你","userName":"ria4com","digg":0,"diggArr":[],"parentUserName":null,"parentNickName":null,"avatar":"https://profile.csdnimg.cn/B/2/3/3_ria4com","nickName":"低代码游戏开发","dateFormat":"1 小时前","tag":"","parentTag":null,"years":null,"vip":null,"vipIcon":null,"companyBlog":null,"companyBlogIcon":null,"flag":null,"flagIcon":null,"levelIcon":null},
"sub":[],
"pointCommentId":null
},
{
"info":
{"commentId":14922981,"articleId":113663811,"parentId":0,"postTime":"2021-02-05 15:06:21","content":"大佬无敌强","userName":"weixin_44671737","digg":0,"diggArr":[],"parentUserName":null,"parentNickName":null,"avatar":"https://profile.csdnimg.cn/E/3/5/3_weixin_44671737","nickName":"兴趣使然的程序猿","dateFormat":"1 月前","tag":"爱码士","parentTag":null,"years":null,"vip":null,"vipIcon":null,"companyBlog":null,"companyBlogIcon":null,"flag":null,"flagIcon":null,"levelIcon":null},
"sub":
[{"commentId":14923083,"articleId":113663811,"parentId":14922981,"postTime":"2021-02-05 15:13:57","content":"感谢支持!一起加油!","userName":"LaoYuanPython","digg":0,"diggArr":[],"parentUserName":"weixin_44671737","parentNickName":"兴趣使然的程序猿","avatar":"https://profile.csdnimg.cn/0/8/1/3_laoyuanpython","nickName":"LaoYuanPython","dateFormat":"1 月前","tag":"爱码士","parentTag":"爱码士","years":null,"vip":null,"vipIcon":null,"companyBlog":null,"companyBlogIcon":null,"flag":null,"flagIcon":null,"levelIcon":null}],
"pointCommentId":null
}
]
}
}"""
>>> response = json.loads(text)
>>> print(response['code'],response['data']['count'],response['data']['list'][0]['info']['nickName'],response['data']['list'][0]['info']['content'])
200 47 低代码游戏开发 恭喜你
>>>
可以看到,上面案例中,将JSON文本转换成Python字典对象只需要import函数loads,再使用loads执行反序列化即可,用2行代码完成了JSON文本到字典类型的转换。
本文介绍了使用Python内置模块JSON将JSON格式文本转换成Python标准字典类型的方法和案例,通过JSON模块的loads即可简单快速完成JSON文本的处理,生成标准的Python对象,方便后续程序的访问。
如果阅读本文于您有所获,敬请点赞、评论、收藏,谢谢大家的支持!
如果对文章内容存在疑问,可以在博客评论区留言,或关注:老猿Python 微信公号发消息咨询。
更多Python爬虫入门的介绍请参考专栏《Python爬虫入门 》
专栏网址:https://blog.csdn.net/laoyuanpython/category_10762553.html
前两个专栏都适合有一定Python基础但无相关知识的小白读者学习,第三个专栏请大家结合《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的学习使用。
对于缺乏Python基础的同仁,可以通过老猿的免费专栏《https://blog.csdn.net/laoyuanpython/category_9831699.html 专栏:Python基础教程目录)从零开始学习Python。
如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。