Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将转储的JSON文件读回Python_Python_Json - Fatal编程技术网

无法将转储的JSON文件读回Python

无法将转储的JSON文件读回Python,python,json,Python,Json,我正在尝试加载一个JSON文件,该文件是我从另一个JSON文件复制内容创建的。但是我不断得到错误ValueError:Expecting属性名:line 1 column 1(char 1)当我试图从复制所有数据的文件中读取JSON数据时,我的JSON数据的格式是 { "server": { "ipaddress": "IP_Sample", "name": "name_Sample", "type": "type_Sample",

我正在尝试加载一个JSON文件,该文件是我从另一个JSON文件复制内容创建的。但是我不断得到错误
ValueError:Expecting属性名:line 1 column 1(char 1)
当我试图从复制所有数据的文件中读取JSON数据时,我的JSON数据的格式是

{
    "server": {
        "ipaddress": "IP_Sample",
        "name": "name_Sample",
        "type": "type_Sample",
        "label": "label_Sample",
        "keyword": "kwd_Sample",
        "uid": "uid_Sample",
        "start_time": "start_Sample",
        "stop_time": "stop_Sample"
    }
}
我的加载和写入方法是

def load(self, filename):
    inputfile = open(filename,'r')
    self.data = json.loads(inputfile.read())
    print (self.data)
    inputfile.close()
    return

def write(self, filename):
    file = open(filename, "w")
    tempObject = self.data
    print type(tempObject)
    #json.dump(filename, self.data)
    print self.data["server"]
    print >> file, self.data
    file.close()
    return

我不知道哪里出了问题,有人能帮我吗?

要在文件中保存和加载JSON,请使用open file对象。您的代码表示您试图将文件名保存到
self.data
,该文件不是fileobject

以下代码起作用:

def write(self, filename):
    with open(filename, 'w') as output:
        json.dump(self.data, output)

def load(self, filename):
    with open(filename, 'r') as input:
        self.data = json.load(input)
我将打开的文件用作上下文管理器,以确保在完成读写操作时关闭它们

您的另一个尝试是,
print>>文件self.data
,它只是将python表示形式打印到文件中,而不是JSON:

>>> print example
{u'server': {u'uid': u'uid_Sample', u'keyword': u'kwd_Sample', u'ipaddress': u'IP_Sample', u'start_time': u'start_Sample', u'label': u'label_Sample', u'stop_time': u'stop_Sample', u'type': u'type_Sample', u'name': u'name_Sample'}}
从文件中读回时,将显示您指示的错误消息:

>>> json.loads("{u'server': {u'uid': u'uid_Sample', u'keyword': u'kwd_Sample', u'ipaddress': u'IP_Sample', u'start_time': u'start_Sample', u'label': u'label_Sample', u'stop_time': u'stop_Sample', u'type': u'type_Sample', u'name': u'name_Sample'}}")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 171, in JSONObject
    raise ValueError(errmsg("Expecting property name", s, end))
ValueError: Expecting property name: line 1 column 1 (char 1)

您需要发布收到的错误。我不知道“错误”是什么,但您的JSON不正确,请修复以下行:
“停止时间”:“停止样本”,
删除
编辑以使数据成为有效的JSON,并列出错误
>>> print json.dumps(example)
'{"server": {"uid": "uid_Sample", "keyword": "kwd_Sample", "ipaddress": "IP_Sample", "start_time": "start_Sample", "label": "label_Sample", "stop_time": "stop_Sample", "type": "type_Sample", "name": "name_Sample"}}'