python - 在 Django : How to serialize dict object to json?

标签 python django json serialization

我有这个非常基本的问题,

>>> from django.core import serializers
>>> serializers.serialize("json", {'a':1})
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/django/core/serializers/__init__.py", line 87, in serialize
    s.serialize(queryset, **options)
  File "/usr/lib/pymodules/python2.6/django/core/serializers/base.py", line 40, in serialize
    for field in obj._meta.local_fields:
AttributeError: 'str' object has no attribute '_meta'
>>> 

如何做到这一点?

最佳答案

此外,由于您似乎使用的是 Python 2.6,因此您可以直接使用 json 模块:

import json
data = json.dumps({'a': 1})

关于python - 在 Django : How to serialize dict object to json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3010920/

相关文章:

python - 我如何实时更新pygame中的时间

Django 信号不适用于我的目录结构

python - django 在 View 和模板中使用 url 参数

python - 如何通过python在txt中的上一行后面添加字符串?

python - Pandas Dataframe Groupby 连接字符串,同时保留字符串的顺序

python - 在 Python 中将十六进制字符串转换为整数

python - django 应用程序中的每个 url 只呈现 home.html

python - 如何解决类型错误: 'numpy.ndarray' object is not callable on Python

java - JsonPath : filter by value in array

json - Swift 中使用 Campaign Monitor 进行 HTTP 身份验证请求