我想在 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/