简体   繁体   中英

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. 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. How can I do this?

That looks like JSON data. You should always pass a raw JSON string to the json.loads function, which will convert JSON into a Python dictionary. JSON's null will be automatically converted to Python's 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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