I have this JSON file:
我有这个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"
}
]
I wrote this script:
我写了这个脚本:
with open('C:/Users/L30607/Desktop/FYP/FourthMay-AllStudents-*.json') as json_data:
d = json.load(json_data)
json_data.close()
pprint(d)
How do I parse the file and extract the array?
如何解析文件并解压缩数组?
I want to get:
我想得到:
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"}]
4 个解决方案
#1
2
Your JSON is not formatted properly. I put validated it in https://jsonformatter.curiousconcept.com/ and it shows that your keys are not wrapped in quotes. This is what it should be:
您的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"
}
]
I re-ran your code and it worked well.
我重新运行了你的代码,它运行良好。
#2
0
Your json should be like :
你的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"
}]
String should be inside quotes. Then only it becomes as valid JSON. You can validate your JSON : Validate JSON
字符串应该在引号内。然后只有它变成有效的JSON。您可以验证您的JSON:验证JSON
#3
0
Always validate your json from http://jsonlint.com/ if you get any errors. In your case you are not giving Keys as string.
如果您遇到任何错误,请始终从http://jsonlint.com/验证您的json。在您的情况下,您没有将Keys作为字符串。
#4
0
This is the error:
这是错误:
Traceback (most recent call last): File "C:/Users/L30607/PycharmProjects/untitled1/333.py", line 36, in d = json.load(json_data) File "C:\Python27\lib\json__init__.py", line 291, in load **kw) File "C:\Python27\lib\json__init__.py", line 339, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 3 column 1 (char 4)
回溯(最近一次调用最后一次):文件“C:/Users/L30607/PycharmProjects/untitled1/333.py”,第36行,在d = json.load(json_data)文件“C:\ Python27 \ lib \ json__init __。py “,第291行,加载** kw)文件”C:\ Python27 \ lib \ json__init __。py“,第339行,在加载中返回_default_decoder.decode(s)文件”C:\ Python27 \ lib \ json \ decoder。 py“,第364行,在解码obj中,end = self.raw_decode(s,idx = _w(s,0).end())文件”C:\ Python27 \ lib \ json \ decoder.py“,第380行,在raw_decode obj中,end = self.scan_once(s,idx)ValueError:期望属性名称:第3行第1列(char 4)
#1
2
Your JSON is not formatted properly. I put validated it in https://jsonformatter.curiousconcept.com/ and it shows that your keys are not wrapped in quotes. This is what it should be:
您的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"
}
]
I re-ran your code and it worked well.
我重新运行了你的代码,它运行良好。
#2
0
Your json should be like :
你的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"
}]
String should be inside quotes. Then only it becomes as valid JSON. You can validate your JSON : Validate JSON
字符串应该在引号内。然后只有它变成有效的JSON。您可以验证您的JSON:验证JSON
#3
0
Always validate your json from http://jsonlint.com/ if you get any errors. In your case you are not giving Keys as string.
如果您遇到任何错误,请始终从http://jsonlint.com/验证您的json。在您的情况下,您没有将Keys作为字符串。
#4
0
This is the error:
这是错误:
Traceback (most recent call last): File "C:/Users/L30607/PycharmProjects/untitled1/333.py", line 36, in d = json.load(json_data) File "C:\Python27\lib\json__init__.py", line 291, in load **kw) File "C:\Python27\lib\json__init__.py", line 339, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 3 column 1 (char 4)
回溯(最近一次调用最后一次):文件“C:/Users/L30607/PycharmProjects/untitled1/333.py”,第36行,在d = json.load(json_data)文件“C:\ Python27 \ lib \ json__init __。py “,第291行,加载** kw)文件”C:\ Python27 \ lib \ json__init __。py“,第339行,在加载中返回_default_decoder.decode(s)文件”C:\ Python27 \ lib \ json \ decoder。 py“,第364行,在解码obj中,end = self.raw_decode(s,idx = _w(s,0).end())文件”C:\ Python27 \ lib \ json \ decoder.py“,第380行,在raw_decode obj中,end = self.scan_once(s,idx)ValueError:期望属性名称:第3行第1列(char 4)