Python字典与json的异同

在学习Python的时候,我们学习到,Python3 的标准数据类型有:

数字(Number) 字符串(String) 列表(List) 元组(Tumple) 集合(Set) 字典(Dictionary)

发现字典和web前端中的json有点像。接下来比较一下两者的异同。

字典结构:

{'user_id': '9593558', 
'page_badge': [['/help/badges/2/student', 'Student', '1'], ['/help/badges/31/commentator', 'Commentator', '1'], ['/help/badges/3/editor', 'Editor', '1'], ['/help/badges/10/scholar', 'Scholar', '1']],
'answer_count': 0, 'accept_answer_count': 0, 
'post_answer_score': 0, 'question_count': 4, 
'favorite_question_count': 0, 'post_question_score': 2}

json数据形式:

{
"studentInfo":
{
    "id":123456,
    "stu_name":"Dorra"
}

}

从形式上看,都是“Key:Value”的形式。

但是从本质上讲,字典是一种数据结构,而json是一种格式;字典有很多内置函数,有多种调用方法,而json是数据打包的一种格式,并不像字典具备操作性,并且是格式就会有一些形式上的限制,比如json的格式要求必须且只能使用双引号作为key或者值的边界符号,不能使用单引号,而且“key”必须使用边界符(双引号),但字典就无所谓了。

Python中提供了json.load()转换函数,方便json数据的调用。

with open("D:/stackoverflow_json/test.json") as json_file:
for curr_line_json in json_file.readlines():
    curr_line_json = json.loads(curr_line_json)
原文地址:https://www.cnblogs.com/MaggieForest/p/12176824.html