在获取网页报文时,经常遇到JSON格式的文本。如:
```html
{"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
}
\]
}
}
```
可以看到这个数据是比较复杂的,但整体就是一个符合JSON格式要求的文本。
针对该报文需要进行数据分析时,如果将其转换成标准字典类型,则数据访问代码好实现,可以利用Python的内置JSON模块将上述文本转换成了字典类型,再进行数据解析,真的好用。
```python
>>> 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
}
\]
}
}"""
>>> 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** 微信公号发消息咨询。
本文转转自微信公众号老猿Python原创https://mp.weixin.qq.com/s/wsgSkvub0RSKwKt6kwq21g,如有侵权,请联系删除。