简体   繁体   English

web2py-将ajax空值转换为Nonetype

[英]web2py - translate ajax null value into Nonetype

When I save something on a page, it sends an AJAX with all the info I need to record in the database. 当我在页面上保存某些内容时,它会发送一个AJAX以及我需要记录在数据库中的所有信息。 It seems much like this: 看起来很像这样:

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

But the null types, when received by the controller, become string types. 但是,当空类型被控制器接收时,它们将成为字符串类型。 I need them to become a NoneType before saving. 保存之前,我需要它们成为NoneType。 How can I do this? 我怎样才能做到这一点?

That looks like JSON data. 看起来像JSON数据。 You should always pass a raw JSON string to the json.loads function, which will convert JSON into a Python dictionary. 您应该始终将原始JSON字符串传递给json.loads函数,该函数会将JSON转换为Python字典。 JSON's null will be automatically converted to Python's None . JSON的null将自动转换为Python的None

For example: 例如:

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM