web2py将ajax空值转换为Nonetyp

2024-04-26 23:49:16 发布

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

当我在页面上保存一些东西时,它会发送一个AJAX,其中包含我需要记录在数据库中的所有信息。好像是这样的:

{type: "cover", title: "test", description: null, tags: null}

但是当控制器接收到空类型时,它就变成了字符串类型。我需要他们在存钱之前变成一个无名小卒。我该怎么做?你知道吗


Tags: 字符串test信息数据库类型titletype记录
1条回答
网友
1楼 · 发布于 2024-04-26 23:49:16

看起来像是JSON数据。您应该始终将原始JSON字符串传递给json.loads文件函数,该函数将JSON转换为Python字典。JSON的null将自动转换为Python的None。你知道吗

例如:

>>> import json

>>> json_to_dict = json.loads(r'{"type": "cover", "title": "test", "description": null, "tags": null}')

>>> print json_to_dict
{u'tags': None, u'type': u'cover', u'description': None, u'title': u'test'}

>>> print json_to_dict['type']
cover

>>> print json_to_dict['tags']
None

相关问题 更多 >