我有这个JSON文件:
[
{
id: "29",
name: "abc",
email: "def@school.edu",
data: "2016-05-03"
},
{
id: "17",
name: "ghi",
email: "jkl@school.edu",
data: "2016-05-12"
},
{
id: "12",
name: "qwe",
email: "cde@school.edu",
data: "2016-04-11"
}
]
我写了这个剧本:
with open('C:/Users/L30607/Desktop/FYP/FourthMay-AllStudents-stackoverflow.json') as json_data:
d = json.load(json_data)
json_data.close()
pprint(d)
如何解析文件并解压缩数组?
我想得到:
d = [{id: "29",name: "abc",email: "def@school.edu",data: "2016-05-03"},{id: "17",name: "ghi",email: "jkl@school.edu",data: "2016-05-12"},{id: "12",name: "qwe",email: "cde@school.edu",data: "2016-04-11"}]
答案 0 :(得分:2)
您的JSON格式不正确。我把它验证在https://jsonformatter.curiousconcept.com/中,它表明你的密钥没有用引号括起来。这应该是:
[
{
"id":"29",
"name":"abc",
"email":"def@school.edu",
"data":"2016-05-03"
},
{
"id":"17",
"name":"ghi",
"email":"jkl@school.edu",
"data":"2016-05-12"
},
{
"id":"12",
"name":"qwe",
"email":"cde@school.edu",
"data":"2016-04-11"
}
]
我重新运行了您的代码并且运行良好。
答案 1 :(得分:0)
这是错误:
追踪(最近一次通话): 文件“C:/Users/L30607/PycharmProjects/untitled1/333.py”,第36行,in d = json.load(json_data) 加载文件“C:\ Python27 \ lib \ json__init __。py”,第291行 ** KW) 文件“C:\ Python27 \ lib \ json__init __。py”,第339行,在加载中 return _default_decoder.decode(s) 在解码中文件“C:\ Python27 \ lib \ json \ decoder.py”,第364行 obj,end = self.raw_decode(s,idx = _w(s,0).end()) 在raw_decode中输入文件“C:\ Python27 \ lib \ json \ decoder.py”,第380行 obj,end = self.scan_once(s,idx) ValueError:期望属性名称:第3行第1列(char 4)
答案 2 :(得分:0)
如果您遇到任何错误,请始终从http://jsonlint.com/验证您的json。在您的情况下,您不是将 Keys 作为字符串。
答案 3 :(得分:0)
你的json应该像:
[{
"id": "29",
"name": "abc",
"email": "def@school.edu",
"data": "2016-05-03"
}, {
"id": "17",
"name": "ghi",
"email": "jkl@school.edu",
"data": "2016-05-12"
}, {
"id": "12",
"name": "qwe",
"email": "cde@school.edu",
"data": "2016-04-11"
}]
字符串应该在引号内。然后只有它变成有效的JSON。 您可以验证您的JSON:Validate JSON