V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
SP00F
V2EX  ›  Python

Python 3.6.X JSON 转 str 无效无错

  •  
  •   SP00F · 2017-11-14 15:32:20 +08:00 · 2296 次点击
    这是一个创建于 2354 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import json
    a = '{"t": 1, "c": "a"}'
    a = json.loads(a)
    print(a["t"])
    

    这样对字符串转 dict 是正常的。

    但是如果

    import json
    a = "{'t': 1, 'c': 'a'}"
    a = json.loads(a)
    

    会报json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

    import json
    a = "{'t': 1, 'c': 'a'}"
    a = json.dumps(a)
    a = json.loads(a)
    print(a["t"])
    

    在进行loads()的时候没有报错,但是在调用的时候报TypeError: string indices must be integers

    看了一圈资料有点懵,JSON 是不允许用单引号括起 key 的,Python 的 JSON 模块可以dumps转一下再loads但是就有点没理解了

    15 条回复    2017-11-18 18:58:50 +08:00
    goofool
        1
    goofool  
       2017-11-14 15:41:18 +08:00   ❤️ 1
    dumps 出去是字符串
    loads 回来还是字符串
    SP00F
        2
    SP00F  
    OP
       2017-11-14 15:44:49 +08:00
    @goofool #1
    对单引号的字符串转 dict 用 dumps 再 loads 一下是不对的吗?
    fasling
        3
    fasling  
       2017-11-14 15:47:00 +08:00   ❤️ 1
    a = "{'t': 1, 'c': 'a'}"
    这里的 a 并不是一个 json 串。json 得是双引号。你第二段的 json.dumps 和 json.loads 写了没有任何作用。
    pixstone
        4
    pixstone  
       2017-11-14 15:47:04 +08:00   ❤️ 1
    ```python
    >>> import json
    >>> a = "{'t': 1, 'c': 'a'}"
    >>> a = json.dumps(a)
    >>> a
    '"{\'t\': 1, \'c\': \'a\'}"'
    >>>
    >>> a = json.loads(a)
    >>> a
    "{'t': 1, 'c': 'a'}"
    >>>
    ```
    goofool
        5
    goofool  
       2017-11-14 15:47:38 +08:00
    @SP00F dumps 出去是个整体的字符串,单引号还是单引号,loads 回来还是不认单引号
    pixstone
        6
    pixstone  
       2017-11-14 15:48:46 +08:00
    a = "{'t': 1, 'c': 'a'}" 是 str 不是 dict
    所以你一顿操作后,还是 str

    dict 应该写成
    a = {'t': 1, 'c': 'a'}
    SP00F
        7
    SP00F  
    OP
       2017-11-14 15:50:22 +08:00
    @fasling
    @pixstone
    @goofool

    多谢各位,一直误以为 dumps 可以处理单引号的问题。
    SP00F
        8
    SP00F  
    OP
       2017-11-14 15:50:52 +08:00
    @pixstone #6 dict 是这样写没错。。我是要把 str 转 dict。。。
    soulomoon
        9
    soulomoon  
       2017-11-14 15:53:08 +08:00   ❤️ 1
    看下报错不就好了吗。。。
    json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line:
    这个是人家 json loader 不认单引号作为属性。

    TypeError: string indices must be integers:
    这个是你用 a["t"]引用的报错,他说你的 a 是个字符串.
    SP00F
        10
    SP00F  
    OP
       2017-11-14 15:54:15 +08:00
    @soulomoon #9 是的。。就是我以为 dumps 可以解决单引号的问题。。。
    fy
        11
    fy  
       2017-11-14 16:03:10 +08:00
    呃 你那个单引号的字符串,不是 JSON 格式。所以不存在 json 转 dict 的问题。
    wuqiangroy
        12
    wuqiangroy  
       2017-11-14 16:58:08 +08:00
    双引号直接用 eval(a)就可以了。
    json 不能对 loads 双引号。
    你 dumps 前是 str,loads 之后也是 str。
    est
        13
    est  
       2017-11-14 17:11:23 +08:00
    ast.literal_eval 通杀单引号双引号 u'' 引号
    testcount
        14
    testcount  
       2017-11-14 17:54:59 +08:00   ❤️ 1
    如果没有特别极致的性能要求,看到个 demjson 好像还不错。

    mingyun
        15
    mingyun  
       2017-11-18 18:58:50 +08:00
    @testcount 这个库可以的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3528 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:48 · PVG 12:48 · LAX 21:48 · JFK 00:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.