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
jayjing
V2EX  ›  Python

请教关于 json 提取数据的问题

  •  1
     
  •   jayjing · 2017-08-10 02:25:58 +08:00 · 2332 次点击
    这是一个创建于 2465 天前的主题,其中的信息可能已经有所发展或是发生改变。
    3.json
    "{\"message\":null,\"result\":{\"data\":[{\"id\":\"1700519714\",\"type\":\"ADD\",\"keyWord\":\"999999\",\"userId\":\"604B868F7C39427AAB651BF2234DE5A6\",\"createTm\":\"2017/07/24\",\"sourceCode\":\"ODR\",\"thisPoint\":\"90\",\"detailDesc\":\"test\",\"curTotalPoint\":\"597\"}],\"totalCount\":1,\"pageSize\":5,\"pageNo\":1,\"totalPage\":1,\"curPoint\":597},\"status\":\"1\",\"success\":true,\"errorcode\":null}"

    需要提取 keyWord 999999

    import json
    f = open('C:/3.json')
    a = f.readline()
    b = json.loads(a)
    d = b['message\\']
    print d
    f.close()

    运行发现
    C:\Users\Administrator\Desktop>C:\Users\Administrator\Desktop\json\1\test.py
    Traceback (most recent call last):
    File "C:\Users\Administrator\Desktop\json\1\test.py", line 5, in <module>
    d = b['message\\']
    TypeError: string indices must be integers

    一步一步排查发现 b 为<type 'unicode'>类型 是不是 b 应该为 dict 类型才能正常输出。请问大牛们这个问题得怎么解决,感激不尽。
    10 条回复    2017-08-10 23:14:40 +08:00
    jayjing
        1
    jayjing  
    OP
       2017-08-10 03:45:08 +08:00   ❤️ 1
    已经解决,感谢。
    anyele
        2
    anyele  
       2017-08-10 07:54:24 +08:00 via Android
    @jayjing 不写出怎么解决的?
    957204459
        3
    957204459  
       2017-08-10 09:15:45 +08:00 via iPhone
    对,怎么不写出怎么解决的?
    hand515
        4
    hand515  
       2017-08-10 09:19:29 +08:00
    message\\ 这个就有问题了吧
    mxmai
        5
    mxmai  
       2017-08-10 09:25:07 +08:00
    ```
    import json

    with open('D:/data/py/data/3.json') as f:
    src = f.readline()
    s = src.decode('string-escape').strip('"')
    d = json.loads(src)

    print s
    print type(d)
    ```
    数据被转义了。

    * https://stackoverflow.com/questions/22600128/json-loads-is-returning-a-unicode-object-instead-of-a-dictionary
    jayjing
        6
    jayjing  
    OP
       2017-08-10 11:11:15 +08:00
    b = json.loads(a) 后发现 b 被存为 unicode 类型 需要转成 dict 类型才能读取出来,这里遇到困难了,怀疑是我的 json 格式不对 导致识别不了 dict
    最后放弃了这个方法。
    后面我是用 json 保存为 txt 用 re.findall 取我要的字符串。
    strict
        7
    strict  
       2017-08-10 11:55:05 +08:00
    null 跟 true 都不是 python 对象啊,怎么 loads 出来?,我很疑惑
    hellove1985
        8
    hellove1985  
       2017-08-10 11:59:46 +08:00
    @strict 这两个有什么关系,为什么不能 loads 出来?,我很疑惑
    strict
        9
    strict  
       2017-08-10 13:22:37 +08:00
    @hellove1985 这个我理解错了,我以为 json 当中的值只能是字符串,其实也可以是 ture,false,null
    mingyun
        10
    mingyun  
       2017-08-10 23:14:40 +08:00
    发现 json.loads 后 null 变成了 None
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5035 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:57 · PVG 11:57 · LAX 20:57 · JFK 23:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.