【问题标题】:web2py - translate ajax null value into Nonetypeweb2py - 将 ajax 空值转换为 Nonetype
【发布时间】:2014-08-14 17:09:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript python ajax web2py


    【解决方案1】:

    这看起来像 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2012-02-18
      • 2011-04-25
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 2011-11-15
      相关资源
      最近更新 更多