值错误。无法加载我刚刚在Python2中转储的json.dumped文件

值错误。无法加载我刚刚在Python2中转储的json.dumped文件,python,json,Python,Json,在下面的代码中,我将一个文件转储到一个.json文件中,然后再次读取它以创建响应,得到一个ValueError with open(key, 'w+') as outfile: if content_as_json: json.dump(file, outfile) response = self._createStoreResponse(status_code=200, content = json.load(outfile)) else:

在下面的代码中,我将一个文件转储到一个.json文件中,然后再次读取它以创建响应,得到一个ValueError

with open(key, 'w+') as outfile:
    if content_as_json:
        json.dump(file, outfile)
        response = self._createStoreResponse(status_code=200, content =  json.load(outfile))
    else:
        outfile.write(file)
        response = self._createStoreResponse(status_code=200, content = outfile.readlines())
追踪在底部。我可以确认所讨论的文件是实际创建的。如果我使用cat,它看起来像一个有效的json文件。见:

/nubera$ cat test_svx-environment.json                                                                                                                              │
{"type": "test_sv", "name": "test_svx-environment", "description": "Testing ground environment of test_sv"}

event: None                                                                                                                                                                                        │
__format_message event: None                                                                                                                                                                       │
event: unknown, log_type: info, message: No document found yet. Creating document with data: {"type": "test_sv", "name": "test_svx-environment", "description": "Testing ground environment of test│
_sv"}                                                                                                                                                                                              │
event: None                                                                                                                                                                                        │
__format_message event: None                                                                                                                                                                       │
event: unknown, log_type: info, message: LocalFileSystemStore: Doing put on:/database/nubera . For File: {"type": "test_sv", "name": "test_svx-environment", "description": "Testing ground environ│
ment of test_sv"}                                                                                                                                                                                  │
[2019-05-22 10:38:56,452] ERROR in app: Exception on /types/test_sv [POST]                                                                                                                         │
Traceback (most recent call last):                                                                                                                                                                 │
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request                                                                                                  │
    rv = self.dispatch_request()                                                                                                                                                                   │
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request                                                                                                       │
    return self.view_functions[rule.endpoint](**req.view_args)                                                                                                                                     │
  File "/usr/local/lib/python2.7/site-packages/flask_restplus/api.py", line 325, in wrapper                                                                                                        │
    resp = resource(*args, **kwargs)                                                                                                                                                               │
  File "/usr/local/lib/python2.7/site-packages/flask/views.py", line 88, in view                                                                                                                   │
    return self.dispatch_request(*args, **kwargs)                                                                                                                                                  │
  File "/usr/local/lib/python2.7/site-packages/flask_restplus/resource.py", line 44, in dispatch_request                                                                                           │
    resp = meth(*args, **kwargs)                                                                                                                                                                   │
  File "./api/types_api.py", line 61, in post                                                                                                                                                      │
  File "./manager/document_manager.py", line 37, in write_type_document                                                                                                                            │
  File "./configuration_store/local_file_system_store.py", line 68, in put                                                                                                                         │
  File "/usr/local/lib/python2.7/json/__init__.py", line 291, in load                                                                                                                              │
    **kw)                                                                                                                                                                                          ├─────────────────────────────────────────
  File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads                                                                                                                             │ sven  ~  .ssh  
    return _default_decoder.decode(s)                                                                                                                                                              │
  File "/usr/local/lib/python2.7/json/decoder.py", line 364, in decode                                                                                                                             │
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())                                                                                                                                              │
  File "/usr/local/lib/python2.7/json/decoder.py", line 382, in raw_decode                                                                                                                         │
    raise ValueError("No JSON object could be decoded")                                                                                                                                            │
ValueError: No JSON object could be decoded  

在从文件加载JSON之前,您需要正确地编写它。意思是:

  • 您的
    with
    上下文需要完成,以便调用
    outfile.close()
  • 您在读取数据之前已刷新数据
在使用
json.load
之前,还需要打开文件句柄进行读取

with open(key, 'w+') as outfile:
    json.dump(file, outfile)

with open(key, 'r') as infile:
    reread_data = json.load(infile)  

cat test\u svx-environment.json
末尾的“u”是什么?复制/粘贴错误,很抱歉,在转储
文件之前,能否向我们展示该文件的外观?它是一个对象还是一个流?啊,你需要在加载它之前先关闭outfile句柄。我会发布一个答案,我不确定这是否完全正确。问题是文件指针;可能您可以在使用
outfile.seek(0)
@DanielRoseman写入数据后重置它,只要数据已刷新到磁盘(这是我的观点)。打电话给flush或close会让你达到目的。我的例子是明确的。