Python 3:不是JSON serializab

2024-05-08 01:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

TypeError: b'Pizza is a flatbread generally topped with tomato sauce and cheese and baked in an oven. It is commonly topped with a selection of meats, vegetables and condiments. The term was first recorded in the 10th century, in a Latin manuscript from Gaeta in Central Italy. The modern pizza was invented in Naples, Italy, and the dish and its variants have since become popular in many areas of the world.\nIn 2009, upon Italy\'s request, Neapolitan pizza was safeguarded in the European Union as a Traditional Speciality Guaranteed dish. The Associazione Verace Pizza Napoletana (the True Neapolitan Pizza Association) is a non-profit organization founded in 1984 with headquarters in Naples. It promotes and protects the "true Neapolitan pizza".\nPizza is sold fresh, frozen or in portions, and is a common fast food item in North America and the United Kingdom. Various types of ovens are used to cook them and many varieties exist. Several similar dishes are prepared from ingredients commonly used in pizza preparation, such as calzone and stromboli.' is not JSON serializable

我有一个程序将其添加到JSON字符串中,这对大多数文本字符串都很好,但显然不是这个。你能告诉我为什么不,或者怎么解决吗?


Tags: andoftheiniswithitcommonly
2条回答

请考虑安装并使用simplejson,它可以处理除unicode之外的字节字符串,要安装它,请使用以下命令:

pip3 install simplejson

代码中的用法:

import simplejson as json

json.dumps({b'name': b'dev'})

这不是字符串,而是字节序列。JSON只知道如何处理Unicode字符串,而不知道字节序列。要么转换成Unicode(json.dumps(x.decode("utf-8"))),要么转换成整数数组(json.dumps(list(x)))。

相关问题 更多 >