python - 'str' 对象没有属性 '__dict__'

标签 python json serialization dictionary

我想在 Python 中将字典序列化为 JSON。我有这个 'str' object has no attribute 'dict' 错误。这是我的代码...

from django.utils import simplejson

class Person(object):
    a = ""

person1 = Person()
person1.a = "111"

person2 = Person()
person2.a = "222"

list = {}
list["first"] = person1
list["second"] = person2

s = simplejson.dumps([p.__dict__ for p in list])

异常(exception)情况是;

Traceback (most recent call last):
  File "/base/data/home/apps/py-ide-online/2.352580383594527534/shell.py", line 380, in post
    exec(compiled_code, globals())
  File "<string>", line 17, in <module>
AttributeError: 'str' object has no attribute '__dict__'

最佳答案

怎么样

s = simplejson.dumps([p.__dict__ for p in list.itervalues()])

关于python - 'str' 对象没有属性 '__dict__',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7498070/

相关文章:

python - 如何将字符串列表转换为数字 numpy 数组?

python - 根据 2D numpy 数组过滤 3D numpy 数组

javascript - D3.js 未在 HTML 中附加更新的树

json - 在 POST 请求中使用 JSON 数组

php - 序列化数据中的正则表达式

python - 在我的 Flask 应用程序中使用 "BOTO"出现 Amazon S3 文件上传问题 -Python

java - XML 作为默认值返回,而不是 JSON 剩余服务

c++ - 将 vector 加载和保存到文件

C# 和 .NET : How to serialize a structure into a byte[] array, 使用 BinaryWriter?

python - 了解 Django Q - 动态