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

标签 python json

我正在尝试加载我的 JSON 文件,该文件是我通过从另一个 JSON 文件复制内容而创建的。但是当我尝试从复制了所有数据的文件中读取 JSON 数据时,我不断收到错误 ValueError: Expecting property name: line 1 column 1 (char 1) ,我的 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 保存到文件或从文件加载 JSON,请使用打开的文件对象。您的代码表明您尝试将文件名保存到 self.data,这不是文件对象...

以下代码有效:

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 >> file, 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.dumps() 输出:

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

关于python - 无法将转储的 JSON 文件读回 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12124884/

相关文章:

python - sklearn.linear_model RidgeCV normalize= 参数到底是做什么的

python - 通过放置一个列表中的第 n 个项目和另一个列表中的其他项目来合并 Python 中的列表?

python - "Can' t load the profile"错误发生在 Python3.5 和 FF48 的 Selenium WebDriver

json - WCF 中具有接口(interface)类型参数的通用返回类型

iphone - 如何手动读取JSON

python - 生成巨大列表的随机排列(在 Python 中)

Python 使用 opencv 从图像创建视频

javascript - AJAX 调用返回数据但失败

c# - 如何在调用 SQL Server 数据库调用的 Web 服务器上运行 AngularJS

java - 将字符串转换为数组再转换为 Observable 中的对象