web2py -翻译ajax空值为Nonetype

web2py - translate ajax null value into Nonetype

本文关键字:Nonetype 空值 ajax 翻译 web2py      更新时间:2023-09-26

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

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

但是当控制器接收到null类型时,它变成字符串类型。我需要它们在保存之前成为NoneType。我该怎么做呢?

这看起来像JSON数据。您应该始终将原始JSON字符串传递给JSON。函数,它将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