【python】json.dumps() 与 json.loads() 用法

一、JSON介绍

JSON代表JavaScript对象符号。它是一种轻量级的数据交换格式,用于存储和交换数据。它是一种独立于语言的格式,非常容易理解,因为它本质上是自描述的。 python中有一个内置包,它支持JSON数据,称为json。 JSON中的数据表示为quoted-strings,由大括号{}之间的键值映射组成。通俗来说就是一种在接口中易于使用的数据处理模块,但是json不属于数据格式。

二、Python和Json数据类型的映射

JSONPython
objectdict
arraylist
stringstr
numberint
trueTrue
falseFalse
nullNone

三、json.load(s)与json.dump(s)区别

json.load:表示读取文件,返回python对象
json.dump:表示写入文件,文件为json字符串格式,无返回
json.dumps:将python中的字典类型转换为字符串类型,返回json字符串 [dict→str]
json.loads:将json字符串转换为字典类型,返回python对象 [str→dict]
load和dump处理的主要是 文件
loads和dumps处理的是 字符串

在这里插入图片描述

json.load()从json文件中读取数据
json.loads()将str类型的数据转换为dict类型
json.dumps()将dict类型的数据转成str
json.dump()将数据以json的数据类型写入文件中
在这里插入图片描述

四、测试

4.1 json.dumps()

import json

data = {
    'fruit':'apple',
    'vegetable':'cabbage'
}
print(data,type(data))

data = json.dumps(data)  # dict转json
print(data,type(data))

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
{"fruit": "apple", "vegetable": "cabbage"} <class 'str'>

4.2 json.loads()

data = """{
"fruit": "apple",
"vegetable": "cabbage"
}"""
# 一般此时data为request.text返回值
print(data, type(data))
data = json.loads(data)
print(data, type(data))

返回:

{
"fruit": "apple",
"vegetable": "cabbage"
} <class 'str'>
{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>

4.3 json.dump()

1.写str
a.py中:

data = "wyt"
with open('b.json', 'w') as f:
    json.dump(data, f)

with open('b.json','r',encoding='utf-8') as f :
    f_str = json.load(f)
    print(f_str,type(f_str))

返回:

wyt <class 'str'>

2.写dict
a.py中:

data = {
    'fruit':'apple',
    'vegetable':'cabbage'
}
with open('b.json', 'w') as f:
    json.dump(data, f)

with open('b.json','r',encoding='utf-8') as f :
    f_str = json.load(f)
    print(f_str,type(f_str))

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>

4.4 json.load()

a.json中存在:

{
"fruit": "apple",
"vegetable": "cabbage"
}

a.py中:

with open('a.json','r',encoding='utf-8') as f :
    f_str = json.load(f)
    print(f_str,type(f_str))

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>

五、报错分析

5.1 本地代码

data = '''{
'fruit':'apple',
'vegetable':'cabbage'
}'''
data = json.loads(data)

5.2 报错返回

在这里插入图片描述

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)

5.3 报错分析与解决

json内部要使用双引号。

data = """{
"fruit": "apple",
"vegetable": "cabbage"
}"""
data = json.loads(data)
print(data, type(data))

返回:

{'fruit': 'apple', 'vegetable': 'cabbage'} <class 'dict'>
  • 30
    点赞
  • 188
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
json.dumps() 是一个Python中的函数,用于将Python对象编码成JSON字符串。它接受一个Python对象作为参数,并返回一个表示该对象的JSON字符串。 它的主要作用是将Python对象转换成JSON字符串形式,方便在网络传输或存储到文件中。它可以接受多个参数,用于控制JSON字符串的生成方式,例如是否跳过特殊键值、是否确保ASCII字符、是否检查循环引用等等。 示例代码中展示了如何使用json.dumps() 将一个字典对象转换成JSON字符串形式。首先,我们定义了一个包含水果和蔬菜的字典对象data,然后使用json.dumps() 函数将该字典对象转换成JSON字符串,并通过print() 函数进行输出。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Pythonjson.dumps()函数](https://blog.csdn.net/m0_51623564/article/details/127339600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [【pythonjson.dumps() 与 json.loads() 用法](https://blog.csdn.net/qq_45859826/article/details/124158012)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微雨停了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值