要在python中加载的.json文件的格式

时间:2014-07-11 08:00:27

标签: python json

这是我的JSON文件

[{"": "", "User ID": "913fd663bc66a452", "Description": "", "Timestamp": "Jul 09, 2014 10:33 PM", "Session Index": "2", "Platform": "Android", "Version": "0.2.9", "Params": "{ email : soleimaniarmin@yahoo.com;  comments : 09337845621;  stars : 4.0;  name : ari}", "Device": "Samsung Galaxy S4", "Event": "feedback"},{"": "", "User ID": "6e35346f9754b787", "Description": "", "Timestamp": "Jul 09, 2014 04:36 PM", "Session Index": "6", "Platform": "Android", "Version": "0.3.0", "Params": "{ email : eddgrow89@hotmail.com;  comments : i love My live ;  stars : 5.0;  name : eddy}", "Device": "Alcatel  One Touch 6033A", "Event": "feedback"},]

我试图在python中解析这个文件,这里是代码:

import json
from pprint import pprint 

with open('android/2014-07-09.json', 'rb') as json_file:
    contents = json_file.read();
    print contents;

data = json.loads(contents);
pprint(data);

但是当我执行它时会发生以下错误..

ValueError: No JSON object could be decoded

如果我将json.loads替换为json.load,则会发生以下错误..

  File "messenger.py", line 9, in <module>
    data = json.load(contents);
  File "/usr/lib/python2.7/json/__init__.py", line 286, in load
    return loads(fp.read(),
  AttributeError: 'str' object has no attribute 'read'

如何解开这个谜团。

2 个答案:

答案 0 :(得分:4)

首先,您应该将文件发送到json.load,而不是将字符串发送到json.loads。存储到变量中然后将其发送到解析器是不必要的开销。

with open('android/2014-07-09.json', 'rb') as json_file:
    contents = json.load(json_file)

然后,您的文件不是有效的json文档。您可以使用任何工具对其进行测试,例如:

在您的情况下,问题是字符串末尾的逗号,就在结束]之前。不仅如此,还有一些其他的&#34;垃圾&#34;在那个json文件中。例如,Params键包含一个字符串作为恰好是json文档本身的值,而不是一些有用的数据。虽然这是有效的json格式,但我无法想到任何可能的情况。

总而言之:更改json文档的来源:)

编辑:正如Martijn Pieters指出的那样,Params实际上并不是一个有效的json字符串,尽管它看起来像那样。但话又说回来......得到一个合适的json文档!

答案 1 :(得分:3)

将字符串放在a JSON validator后表示它无效JSON:

Parse error on line 25:
...edback"    },    ]
--------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

那是因为在最后一个对象之后有一个尾随逗号。 JSON字符串中不允许使用尾随逗号。

如果您无法修复该文件,可以使用以下命令删除此逗号:

import re

contents = re.sub(r',\s*(?=])', '', contents)

演示:

>>> import json
>>> import re
>>> contents = r'[{"": "", "User ID": "913fd663bc66a452", "Description": "", "Timestamp": "Jul 09, 2014 10:33 PM", "Session Index": "2", "Platform": "Android", "Version": "0.2.9", "Params": "{ email : soleimaniarmin@yahoo.com;  comments : 09337845621;  stars : 4.0;  name : ari}", "Device": "Samsung Galaxy S4", "Event": "feedback"},{"": "", "User ID": "6e35346f9754b787", "Description": "", "Timestamp": "Jul 09, 2014 04:36 PM", "Session Index": "6", "Platform": "Android", "Version": "0.3.0", "Params": "{ email : eddgrow89@hotmail.com;  comments : i love My live ;  stars : 5.0;  name : eddy}", "Device": "Alcatel  One Touch 6033A", "Event": "feedback"},]'
>>> json.loads(contents)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>> contents = re.sub(r',\s*(?=])', '', contents)
>>> json.loads(contents)
[{u'': u'', u'Version': u'0.2.9', u'Description': u'', u'Timestamp': u'Jul 09, 2014 10:33 PM', u'Session Index': u'2', u'Platform': u'Android', u'User ID': u'913fd663bc66a452', u'Params': u'{ email : soleimaniarmin@yahoo.com;  comments : 09337845621;  stars : 4.0;  name : ari}', u'Device': u'Samsung Galaxy S4', u'Event': u'feedback'}, {u'': u'', u'Version': u'0.3.0', u'Description': u'', u'Timestamp': u'Jul 09, 2014 04:36 PM', u'Session Index': u'6', u'Platform': u'Android', u'User ID': u'6e35346f9754b787', u'Params': u'{ email : eddgrow89@hotmail.com;  comments : i love My live ;  stars : 5.0;  name : eddy}', u'Device': u'Alcatel  One Touch 6033A', u'Event': u'feedback'}]

另一个选择是使用JSON解码器,可以告诉他忽略这些错误,例如demjson

import demjson

data = demjson.decode(contents, strict=False)

或从文件加载:

with open('android/2014-07-09.json', 'rb') as json_file:
    data = demjson.decode_file(json_file, strict=False)