ValueError。无法将我刚才在json2中转储的json.load文件

时间:2019-05-22 11:11:00

标签: 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:
        outfile.write(file)
        response = self._createStoreResponse(status_code=200, content = outfile.readlines())

追溯在底部。我可以确认该文件实际上是创建的。如果我做一只猫,它看起来像一个有效的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  

1 个答案:

答案 0 :(得分:1)

在可以从文件加载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)