设为首页收藏本站
查看: 89|回复: 0

[PHP] python的json解析函数不支持单引号字符串

[复制链接]

论坛元老

Rank: 6Rank: 6

积分
34274
主题
17031
UID
1347
M币
67
贡献
17176

  • 发表于 2017-5-14 02:44:00 | 显示全部楼层 |阅读模式
    使用python标准库的json解析函数json.loads是不能使用单引号引用的json字符串,示例如下:
    改用双引号,或者在loads之前先调用json.dumps(a)也可以。
    >>> a = "{'aa':'dd','dd':'df'}" >>> import json >>> b = json.loads(a) Traceback (most recent call last): File "", line 1, in  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads return _default_decoder.decode(s) File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode obj, end = self._scanner.iterscan(s, **kw).next() File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan rval, next_pos = action(m, context) File "/usr/lib64/python2.6/json/decoder.py", line 171, in JSONObject raise ValueError(errmsg("Expecting property name", s, end)) ValueError: Expecting property name: line 1 column 1 (char 1) >>> a = '{"aa":"asdf","dd":"dfads"} File "", line 1 a = '{"aa":"asdf","dd":"dfads"} ^ SyntaxError: EOL while scanning string literal >>> a = '{"aa":"asdf","dd":"dfads"}' >>> b = json.loads(a) >>> b >>> {u'aa': u'asdf', u'dd': u'dfads'}
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    在我站开通SVIP可同时获得17个站点VIP资源 立即登录 立即注册
    快速回复 返回顶部 返回列表