gru*_*czy 44 python django serialization json
我在客户端和Django服务器之间发送信息,我想使用JSON.我发送简单的信息 - 字符串列表.我试过用django.core.serializers,但是当我做的时候,我得到了
AttributeError: 'str' object has no attribute '_meta'
Run Code Online (Sandbox Code Playgroud)
看来这只能用于Django对象.如何序列化简单的Python对象?
Emi*_*nov 85
您可以使用纯Python来执行此操作:
import json
list = [1, 2, (3, 4)] # Note that the 3rd element is a tuple (3, 4)
json.dumps(list) # '[1, 2, [3, 4]]'
Run Code Online (Sandbox Code Playgroud)
小智 7
如果使用Python 2.5,您可能需要导入simplejson:
try:
import json
except ImportError:
import simplejson as json
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131094 次 |
| 最近记录: |