相关文章推荐
小眼睛的莲藕  ·  Python strip()方法 - ...·  4 周前    · 
大力的勺子  ·  python ...·  4 周前    · 
豪气的移动电源  ·  mysql ...·  3 周前    · 
低调的打火机  ·  pred=scaler.inverse_tr ...·  1 年前    · 
成熟的充电器  ·  SQL Server ...·  2 年前    · 

当我尝试运行以下代码,来练习使用json.loads()和json.dumps()函数时,系统给我报出了这个错误。

import json
text = {"a":1,"b":2,"c":3}
j = json.loads(text)
print(j)
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    j = json.loads(text)
  File "C:\Users\Reborn\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 348, in loads
    'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

其实解决办法很简单,由于text现在是一个字典,只需要用'''符号将它转换成字符串就可以了。但我们有必要知道loads()和jumps()这两个函数的具体用法,接下来我就简单说一下。

loads()

loads(param)是将文本字符串转换为json对象的函数,其函数名是load string 的缩写,意思是加载字符串。所以其参数param必须要是一个字典型的字符串。且字典的键必须用双引号来包裹。

dumps()

dumps(param)是将json数据对象转换为文本字符串的函数,其函数名是dump string 的缩写,意思是输出字符串,所以其参数param必须要是json对象,也就是loads()函数返回的数据类型。

关注我公众号【小众技术】,此公众号专注分享Python、爬虫学习资料和干货,关注后回复【PYTHON】,无套路免费送你一个学习大礼包,包括爬虫视频和电子书~

当我尝试运行以下代码,来练习使用json.loads()和json.dumps()函数时,系统给我报出了这个错误。import jsontext = {"a":1,"b":2,"c":3}j = json.loads(text)print(j)Traceback (most recent call last): File "test.py", line 5, in &lt;m...
python 的Beautiful Soup 4 扩展库的使用过程中出现了 TypeError : list indices must be integers or slices, not str 这个 错误 ,这里就分析一下为什么会报错以及如何解决。 这个 错误 的意思是’类型 错误 :list的索引必须是’integers’或者’slices’不能是’ str ’ 我出现 错误 的代码: from bs4 import BeautifulSoup #读取页面 soup = BeautifulSoup(open('index.html')) #获取标签 img_tag = div.select(
两个函数: load s(param) load s(param)是将文本字符串转换为 json 对象的函数,其函数名是 load str ing 的缩写,意思是加载字符串。所以其参数param必须要是一个字典型的字符串。且字典的键必须用双引号来包裹。 dump s(param) dump s(param)是将 json 数据对象转换为文本字符串的函数,其函数名是 dump str ing 的缩写,意思是输出字符串,所以其参数param必须要是 json 对象,也就是 load s()函数返回的数据类型。 接口数据: "data": { {'受理法院': '四川省乐山市中级人民法院', 'event_type': '破产清算', 'event_id': '4758105', '公司名称': '乐山乐电天威硅业科技有限责任公司'} TypeError JSON 对象必须是 str byte s或 bytearray ,而不是' dict ' 开始的代码: # 直接对该字段进行解析 data1 = json . load s(data['events'][0]) 修改后的代码: 加入 json . dump s() json . load .
Python 出现 错误 TypeError : ‘NoneType’ object is not iterable 解决办法 TypeError : ‘NoneType’ object is not iterable  这个 错误 提示一般发生在将None赋给多个值时。 def myprocess(): a == b if a != b: return True, value; flag, val = myprocess() 在判断语句中,当if条件不满足,并且没有else语句时,函数默认返回None。 在没有return语句时, Python 也默认会返回None 调用时,将Non
Traceback (most recent call last): File "/home/admin/apps/*-*-sync/task/__init__.py", line 114, in run for data in self.index_data(): File "/home/admin/apps/*-*-sync/task/huahuo/*_*_sync.py", line 72, in index_data data = self.make_field_true(r
åa indef art one abacus n.frame with beads that slide along parallel rods, used for teaching numbers to children, and (in some countries) for counting abandon v. go away from (a person or thing or place) not intending to return; forsake; desert abandonment n. abandoning abase v. ~ oneself/sb lower oneself/sb in dignity; degrade oneself/sb ; abash to de str oy the self-possession or self-confidence of:disconcert abashed adj. ~
json . load s() load s(param)是将文本字符串转换为 json 对象的函数,其函数名是 load str ing 的缩写,意思是加载字符串。所以其参数param必须要是一个字典型的字符串。且字典的键必须用双引号来包裹。 如果是嵌套列表的话,不是 str ,会报错 TypeError : the JSON object must be str , byte s or bytearray , not ' dict 解决办法 dump s() dump s(param)是将 json 数据对象转换为文本字符串的函数,其函
Exception has occurred: TypeError the JSON object must be str , byte s or bytearray , not TextIOWrapper File “/Users/a/exp.py”, line 8, in json _ object = json . load s(open(filedir)) json .decoder. JSON DecodeError: Expecting value: line 1 column 1 (char 0) 这一般是jso
在写入文件的时候,出现: TypeError : write() argument must be str , not dict ,报错。 可以使用 json ,格式写入 import requests import re import json def get_one_page(url): head = { 'User-Agent': 'Mozilla/5.0 (Windows N...
python错误: TypeError: the JSON object must be str, bytes or bytearray, not 'dict'解决办法 ヽ♂ㄝ笹緗垨: Python错误:AttributeError: 'generator' object has no attribute 'next'解决办法 Cyber Striver: Good! Python错误:TypeError: 'int' object is not callable解决办法 月饼kelly: 表情包 太棒了!谢谢作者!刚编完发现有问题哈哈哈~ 表情包 Python错误:ImportError: cannot import name get_column_letter 解决办法 lunabook: 看来我在经历你曾经经历过的每一次。 表情包 谢谢分享。 【已解决】java.sql.SQLIntegrityConstraintViolationException: Duplicate entry ‘xxx‘ for key ‘xxx‘ Springboot怎样进行参数校验?@Validation注解怎么用?分组校验如何实现?