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

求教 json 解析

  •  
  •   cxl008 · 2014-09-04 16:14:52 +08:00 · 3609 次点击
    这是一个创建于 3520 天前的主题,其中的信息可能已经有所发展或是发生改变。
    droidbox 里面 一段
    { "CryptoUsage": { "operation": "encryption", "algorithm": "AES/CBC/PKCS5Padding", "data": "{"model":"Full Android on Emulator","location":"","nettype":"3","manufacturer":"unknown","imei":"115732340242753","activetime":"1409818147","basebandversion":"no message","kernelversion":"2.6.29-gc497e41\nkroot@kennyroot #2\nThu Dec 8 15:07:43 PST 2011","osversion":"4.1.1","operator":"310260000000000","ip":"fe80::5054:ff:fe12:3456%eth0","pix":"320*480","deviceid":"0761AE5246580A7087A39BA0BA48BAA0"}" } }


    因为 data 后面 多了一对 引号 json.loads except 了。。。。有什么办法处理正确啊?
    16 条回复    2014-09-05 10:05:17 +08:00
    keithellis
        1
    keithellis  
       2014-09-04 16:28:46 +08:00
    先格式化一遍 JSON 字符串。。。
    clino
        2
    clino  
       2014-09-04 16:40:43 +08:00
    这个就是错的吧,如果在json字符串中要有"那应该加转义符\吧?
    allenm
        3
    allenm  
       2014-09-04 16:45:05 +08:00
    这个 JSON 字符串本身就是错的。应该从源头着手,用 JSON 标准库来做序列化,尽力不要手工拼接,除非你非常了解 JSON 的规范,并且知道你在干什么。
    no13bus
        4
    no13bus  
       2014-09-04 16:47:35 +08:00
    试试demjson
    cxl008
        5
    cxl008  
    OP
       2014-09-04 16:53:24 +08:00
    @allenm 源头是没办法改了。得到就是这样的串。。。。
    cxl008
        6
    cxl008  
    OP
       2014-09-04 16:53:45 +08:00
    @keithellis 恕我愚钝,如何操作?
    cxl008
        7
    cxl008  
    OP
       2014-09-04 16:57:54 +08:00
    @no13bus 还是不好使
    keithellis
        8
    keithellis  
       2014-09-04 16:59:05 +08:00   ❤️ 1
    JSONRaw.replace('"data": "', '"data": ')
    JSONRaw.replace('"}" } }', '"} } }')
    cxl008
        9
    cxl008  
    OP
       2014-09-04 17:09:08 +08:00
    @keithellis 是个办法啊。。。。。
    wwlweihai
        10
    wwlweihai  
       2014-09-04 17:11:21 +08:00
    百度下 json格式化,然后你就能看到结构,一看就明白
    cxl008
        11
    cxl008  
    OP
       2014-09-04 17:14:55 +08:00
    @wwlweihai 这回答。。。需要关小黑屋啊。。。。。问题里面都说了 这个串错误的原因,不是不了解json结构,是问怎么好的处理方式。 百度是万能的就不来v2了。。。。
    wwlweihai
        12
    wwlweihai  
       2014-09-04 17:39:09 +08:00
    不好意思,解决不了。json,我没有仔细看过json串,
    dong3580
        13
    dong3580  
       2014-09-04 17:42:08 +08:00
    这个明显是json格式有问题的嘛,先把获取到的json个转换成正常的才行啊,
    binux
        14
    binux  
       2014-09-04 17:48:04 +08:00
    如果你用错误的方式用 JSON,人家都不知道你要表达什么,怎么给你解?
    lldld
        15
    lldld  
       2014-09-05 10:01:16 +08:00
    你都知道是多了一对引号了,还不能处理么?
    happywowwow
        16
    happywowwow  
       2014-09-05 10:05:17 +08:00
    怎么能容忍这串错误json的传给你
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1249 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:23 · PVG 07:23 · LAX 16:23 · JFK 19:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.