简体   繁体   English

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

[英]Format of a .json file to be loaded in python

Here is my JSON file 这是我的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"},]

I am trying to parse this file in python here is the code: 我正在尝试在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);

but when I execute it the following error occurs.. 但是当我执行它时,发生以下错误。

ValueError: No JSON object could be decoded

and if I replace json.loads with json.load the following error occurs.. 如果我更换json.loadsjson.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'

How to solve this mystery. 如何解决这个谜。

First of all, you should send the file into the json.load instead of sending the string to json.loads . 首先,您应该将文件发送到json.load而不是将字符串发送到json.loads It is unnecessary overhead to store in into a variable and then send it to the parser. 没有必要的开销来存储到变量中,然后将其发送到解析器。

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

Then, your file is not a valid json document. 然后,您的文件不是有效的json文档。 You can test it with any tool, for instance: 您可以使用任何工具进行测试,例如:

In your case, the problem is a comma at the end of your string, just before the closing ] . 在您的情况下,问题是字符串结尾处的逗号,紧接在]之前。 Not only that, there is some other "garbage" in that json file. 不仅如此,该json文件中还有一些其他“垃圾”。 For instance, the Params key contains a string as a value that happens to be a json document itself, instead of some useful data. 例如, Params键包含一个字符串作为值,恰好是json文档本身,而不是一些有用的数据。 Although that is valid json format, I cant think of any case in which that is intended. 尽管这是有效的json格式,但我无法想到任何打算使用的情况。

All in all: change the source of your json document :) 总而言之:更改json文档的来源:)

EDIT : As Martijn Pieters pointed out, the Params is actually not a valid json string, although it looks like that. 编辑:正如Martijn Pieters指出的那样,尽管它看起来像这样,但Params实际上不是有效的json字符串。 But then again... get a proper json document! 但是再说一次……得到一个正确的json文档!

Putting your string through a JSON validator shows it is not valid JSON: 将字符串通过JSON验证程序显示为无效的JSON:

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

That's because there is a trailing comma after the last object. 这是因为最后一个对象后面有逗号。 Trailing commas are not allowed in a JSON string. JSON字符串中不允许结尾逗号。

If you cannot fix the file, you can remove this comma with: 如果无法修复该文件,则可以使用以下命令删除该逗号:

import re

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

Demo: 演示:

>>> 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'}]

Another option is to use a JSON decoder that can be told to overlook such errors, such as demjson : 另一个选择是使用JSON解码器,可以告诉它忽略此类错误,例如demjson

import demjson

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

or loading from a file: 或从文件加载:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM