Python中的JSON如何用,字符串怎样转换为JSON
Admin 2022-06-21 群英技术资讯 808 次浏览
这篇文章给大家分享的是Python中的JSON如何用,字符串怎样转换为JSON。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。JSON 是 JavaScript Object Notation(JavaScript 对象标记)的缩写。
它是一种数据格式,用于为 Web 应用程序存储和传输信息。
JSON 最初来自 JavaScript 编程语言,但它并不仅仅局限于一种语言。
大多数现代编程语言都有用于解析和生成 JSON 数据的库。
JSON 主要用于在服务器和客户端之间发送和接收数据,其中客户端是网页或 Web 应用程序。
在 Web 应用程序通过网络连接时使用的请求-响应周期中,这是一种更可靠的格式。与复杂且不太紧凑的 XML 相比,JSON 是使用得更多的格式。
在 JSON 中,数据以键值对的形式写入,如下所示:
"first_name": "Katie"
数据用双引号括起来,键值对用冒号分隔。
可以有多个键值对,每个键值对之间用逗号分隔:
"first_name": "Katie", "last_name": "Rodgers"
上面的例子展示了一个对象,一个多个键值对的集合。
对象在花括号内:
{
"first_name": "Katie",
"last_name": "Rodgers"
}
你还可以使用 JSON 创建数组,即值的有序列表。在这种情况下,数组包含在方括号内:
[
{
"first_name": "Katie",
"last_name": "Rodgers"
},
{
"first_name": "Naomi",
"last_name": "Green"
},
]
// or:
{
"employee": [
{
"first_name": "Katie",
"last_name": "Rodgers"
},
{
"first_name": "Naomi",
"last_name": "Green"
},
]
}
//this created an 'employee' object that has 2 records.
// It defines the first name and last name of an employee
要在 Python 中使用 JSON,首先需要在 Python 文件的顶部包含 JSON 模块。这是 Python 内置的,是标准库的一部分。
因此,假设你有一个名为 demo.py 的文件。在顶部,你将添加以下行:
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))
#output
#<class 'str'>
你可以使用 json.loads() 函数将其转换为 Python 中的 JSON。
json.loads() 函数接受有效字符串作为输入并将其转换为 Python 字典。
这个过程叫作反序列化——将字符串转换为对象。
#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))
#output
#<class 'dict'>
然后,你可以访问每个单独的项目,就像使用 Python 字典时一样:
#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))
#output
#<class 'dict'>
#access first_name in dictionary
print(json_object["first_name"])
#output
#Michael
让我们再举一个例子:
1. 取一些 JSON 字符串数据
import json
#json string
employees_string = '''
{
"employees": [
{
"first_name": "Michael",
"last_name": "Rodgers",
"department": "Marketing"
},
{
"first_name": "Michelle",
"last_name": "Williams",
"department": "Engineering"
}
]
}
'''
#check data type using the type() method
print(type(employees_string))
#output
#<class 'str'>
2. 使用 json.loads() 函数将字符串转换为对象
import json
emoloyees_string = '''
{
"employees" : [
{
"first_name": "Michael",
"last_name": "Rodgers",
"department": "Marketing"
},
{
"first_name": "Michelle",
"last_name": "Williams",
"department": "Engineering"
}
]
}
'''
data = json.loads(employees_string)
print(type(data))
#output
#<class 'dict'>
3. 读取数据
import json
employees_string = '''
{
"employees" : [
{
"first_name": "Michael",
"last_name": "Rodgers",
"department": "Marketing"
},
{
"first_name": "Michelle",
"last_name": "Williams",
"department": "Engineering"
}
]
}
'''
data = json.loads(employees_string)
print(type(data))
#output
#<class 'dict'>
#access first_name
for employee in data["employees"]:
print(employee["first_name"])
#output
#Michael
#Michelle
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了教你利用Python+Turtle绘制简易版爱心表白,文中有非常详细的代码示例,对想要和男朋友或者女朋友表白的小伙伴们有很大帮助哟,需要的朋友可以参考下
哈希表或称为散列表,是一种常见的、使用频率非常高的数据存储方案。本文将站在开发者的角度,带着大家一起探究哈希的世界,感兴趣的小伙伴可以跟随小编一起学习一下
本章内容概要1.多层装饰器2.有参装饰器3.递归函数4.算法(二分法)本章内容详解1.多层装饰器1.1什么是多层装饰器多层装饰器是从下往上依次执行,需要注意的是,被装饰的函数名所指代的函数是一直被装饰器中的内层函数所取代。1.2语法糖的功能会自动将下面紧挨着的函数名...
fotmat作为Python的的格式字符串函数,主要通过字符串中的花括号{},来识别替换字段,从而完成字符串的格式化,下面这篇文章主要给大家介绍了关于python中format用法的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Python 第三方opencv库实现图像分割处理,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008