Python 如何把 String 转换为 Json 对象

在我们对 JSON 进行处理的时候,大概率我们会需要把字符串转换为 JSON 对象后才能进行处理。

Python 贴心的使用

json.loads(employee_string)

就可以了。

首先需要做的就是导入 JSON 库。

#include json library import json

对现代程序员来说,JSON 数据结构基本上是非常常见的数据结构了,几乎所有语言都可以处理。

#include json library
import json

#json string data
employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}'

#check data type with type() method
print(type(employee_string))

#convert string to  object
json_object = json.loads(employee_string)

#check new data type
print(type(json_object))

上面的代码就可以直接让 Python 把字符串转换为 JSON 的对象了。

当我们完成转换后,就可以对 JSON 的对象进行相关操作了。

Python 如何把 String 转换为 Json 对象 - Python - iSharkFly

猜你喜欢

转载自blog.csdn.net/huyuchengus/article/details/133047499