json 파일을로드하는 동안 JSONDecodeError 발생 [중복]
Nov 01 2020
test.json다음과 같은 콘텐츠가 있는 json 파일 이 제공됩니다.
{'review/appearance': 2.5, 'beer/style': 'Hefeweizen', 'review/palate': 1.5, 'review/taste': 1.5, 'beer/name': 'Sausa Weizen', 'review/timeUnix': 1234817823, 'beer/ABV': 5.0, 'beer/beerId': '47986', 'beer/brewerId': '10325', 'review/timeStruct': {'isdst': 0, 'mday': 16, 'hour': 20, 'min': 57, 'sec': 3, 'mon': 2, 'year': 2009, 'yday': 47, 'wday': 0}, 'review/overall': 1.5, 'review/text': 'A lot of foam. But a lot.\tIn the smell some banana, and then lactic and tart. Not a good start.\tQuite dark orange in color, with a lively carbonation (now visible, under the foam).\tAgain tending to lactic sourness.\tSame for the taste. With some yeast and banana.', 'user/profileName': 'stcules', 'review/aroma': 2.0}
{'review/appearance': 3.0, 'beer/style': 'English Strong Ale', 'review/palate': 3.0, 'review/taste': 3.0, 'beer/name': 'Red Moon', 'review/timeUnix': 1235915097, 'beer/ABV': 6.2, 'beer/beerId': '48213', 'beer/brewerId': '10325', 'review/timeStruct': {'isdst': 0, 'mday': 1, 'hour': 13, 'min': 44, 'sec': 57, 'mon': 3, 'year': 2009, 'yday': 60, 'wday': 6}, 'review/overall': 3.0, 'review/text': 'Dark red color, light beige foam, average.\tIn the smell malt and caramel, not really light.\tAgain malt and caramel in the taste, not bad in the end.\tMaybe a note of honey in teh back, and a light fruitiness.\tAverage body.\tIn the aftertaste a light bitterness, with the malt and red fruit.\tNothing exceptional, but not bad, drinkable beer.', 'user/profileName': 'stcules', 'review/aroma': 2.5}
{'review/appearance': 3.0, 'beer/style': 'Foreign / Export Stout', 'review/palate': 3.0, 'review/taste': 3.0, 'beer/name': 'Black Horse Black Beer', 'review/timeUnix': 1235916604, 'beer/ABV': 6.5, 'beer/beerId': '48215', 'beer/brewerId': '10325', 'review/timeStruct': {'isdst': 0, 'mday': 1, 'hour': 14, 'min': 10, 'sec': 4, 'mon': 3, 'year': 2009, 'yday': 60, 'wday': 6}, 'review/overall': 3.0, 'review/text': 'Almost totally black. Beige foam, quite compact, not bad.\tLight smell, just a bit of roast, and some hop. A bit too light.\tThe taste is light oo, and drinkable, with some malt, roast, hints of coffee.\tNothing exceptional, but after all drinkable and pleasant.\tLight to average body.\tIn the aftertaste some dust, somr roast, hint of caramel, and a bit of bitterness.\tNo defect, drinkable, not bad.', 'user/profileName': 'stcules', 'review/aroma': 2.5}
다음 코드로 파일을로드했습니다.
import json
with open('./test.json', 'r') as json_file:
data = json.load(json_file)
그러나 오류가 발생 JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)합니다..
다른 옵션 :
data = json.loads('./test.json')
print(data)
산출:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
어떻게 올바르게 할 수 있습니까? 미리 감사드립니다.
답변
2 HarshaBiyani Nov 02 2020 at 06:07
JSON에는 두 가지 문제가 있습니다.
- 배열 대괄호가 없습니다.
[] - 수정
'에"
올바른 JSON :
[{
"review/appearance": 2.5,
"beer/style": "Hefeweizen",
"review/palate": 1.5,
"review/taste": 1.5,
"beer/name": "Sausa Weizen",
"review/timeUnix": 1234817823,
"beer/ABV": 5.0,
"beer/beerId": "47986",
"beer/brewerId": "10325",
"review/timeStruct": {
"isdst": 0,
"mday": 16,
"hour": 20,
"min": 57,
"sec": 3,
"mon": 2,
"year": 2009,
"yday": 47,
"wday": 0
},
"review/overall": 1.5,
"review/text": "A lot of foam. But a lot.\tIn the smell some banana, and then lactic and tart. Not a good start.\tQuite dark orange in color, with a lively carbonation (now visible, under the foam).\tAgain tending to lactic sourness.\tSame for the taste. With some yeast and banana.",
"user/profileName": "stcules",
"review/aroma": 2.0
}, {
"review/appearance": 3.0,
"beer/style": "English Strong Ale",
"review/palate": 3.0,
"review/taste": 3.0,
"beer/name": "Red Moon",
"review/timeUnix": 1235915097,
"beer/ABV": 6.2,
"beer/beerId": "48213",
"beer/brewerId": "10325",
"review/timeStruct": {
"isdst": 0,
"mday": 1,
"hour": 13,
"min": 44,
"sec": 57,
"mon": 3,
"year": 2009,
"yday": 60,
"wday": 6
},
"review/overall": 3.0,
"review/text": "Dark red color, light beige foam, average.\tIn the smell malt and caramel, not really light.\tAgain malt and caramel in the taste, not bad in the end.\tMaybe a note of honey in teh back, and a light fruitiness.\tAverage body.\tIn the aftertaste a light bitterness, with the malt and red fruit.\tNothing exceptional, but not bad, drinkable beer.",
"user/profileName": "stcules",
"review/aroma": 2.5
}, {
"review/appearance": 3.0,
"beer/style": "Foreign / Export Stout",
"review/palate": 3.0,
"review/taste": 3.0,
"beer/name": "Black Horse Black Beer",
"review/timeUnix": 1235916604,
"beer/ABV": 6.5,
"beer/beerId": "48215",
"beer/brewerId": "10325",
"review/timeStruct": {
"isdst": 0,
"mday": 1,
"hour": 14,
"min": 10,
"sec": 4,
"mon": 3,
"year": 2009,
"yday": 60,
"wday": 6
},
"review/overall": 3.0,
"review/text": "Almost totally black. Beige foam, quite compact, not bad.\tLight smell, just a bit of roast, and some hop. A bit too light.\tThe taste is light oo, and drinkable, with some malt, roast, hints of coffee.\tNothing exceptional, but after all drinkable and pleasant.\tLight to average body.\tIn the aftertaste some dust, somr roast, hint of caramel, and a bit of bitterness.\tNo defect, drinkable, not bad.",
"user/profileName": "stcules",
"review/aroma": 2.5
}]
1 buran Nov 02 2020 at 05:59
이것은 json 파일이 아닙니다. 과 같은 ndjson의 타사 패키지 봐 ndjson
또한 작은 따옴표가 아닌 큰 따옴표를 사용해야합니다.
이것이 생성하거나 변경할 수있는 파일 인 경우이 3 개의 JSON 객체를 JSON 배열에 넣을 수 있으며 그러면 t가 유효한 JSON이됩니다.
1 JackTaylor Nov 02 2020 at 06:07
문제는 파일이 유효한 JSON이 아니라는 것입니다. 문자열 주위에 작은 따옴표 대신 큰 따옴표가 필요하며 세 개의 개체를 배열 안에 넣어야합니다.
대신 이것을 시도하십시오.
[
{"review/appearance": 2.5, "beer/style": "Hefeweizen", "review/palate": 1.5, "review/taste": 1.5, "beer/name": "Sausa Weizen", "review/timeUnix": 1234817823, "beer/ABV": 5.0, "beer/beerId": "47986", "beer/brewerId": "10325", "review/timeStruct": {"isdst": 0, "mday": 16, "hour": 20, "min": 57, "sec": 3, "mon": 2, "year": 2009, "yday": 47, "wday": 0}, "review/overall": 1.5, "review/text": "A lot of foam. But a lot.\tIn the smell some banana, and then lactic and tart. Not a good start.\tQuite dark orange in color, with a lively carbonation (now visible, under the foam).\tAgain tending to lactic sourness.\tSame for the taste. With some yeast and banana.", "user/profileName": "stcules", "review/aroma": 2.0},
{"review/appearance": 3.0, "beer/style": "English Strong Ale", "review/palate": 3.0, "review/taste": 3.0, "beer/name": "Red Moon", "review/timeUnix": 1235915097, "beer/ABV": 6.2, "beer/beerId": "48213", "beer/brewerId": "10325", "review/timeStruct": {"isdst": 0, "mday": 1, "hour": 13, "min": 44, "sec": 57, "mon": 3, "year": 2009, "yday": 60, "wday": 6}, "review/overall": 3.0, "review/text": "Dark red color, light beige foam, average.\tIn the smell malt and caramel, not really light.\tAgain malt and caramel in the taste, not bad in the end.\tMaybe a note of honey in teh back, and a light fruitiness.\tAverage body.\tIn the aftertaste a light bitterness, with the malt and red fruit.\tNothing exceptional, but not bad, drinkable beer.", "user/profileName": "stcules", "review/aroma": 2.5},
{"review/appearance": 3.0, "beer/style": "Foreign / Export Stout", "review/palate": 3.0, "review/taste": 3.0, "beer/name": "Black Horse Black Beer", "review/timeUnix": 1235916604, "beer/ABV": 6.5, "beer/beerId": "48215", "beer/brewerId": "10325", "review/timeStruct": {"isdst": 0, "mday": 1, "hour": 14, "min": 10, "sec": 4, "mon": 3, "year": 2009, "yday": 60, "wday": 6}, "review/overall": 3.0, "review/text": "Almost totally black. Beige foam, quite compact, not bad.\tLight smell, just a bit of roast, and some hop. A bit too light.\tThe taste is light oo, and drinkable, with some malt, roast, hints of coffee.\tNothing exceptional, but after all drinkable and pleasant.\tLight to average body.\tIn the aftertaste some dust, somr roast, hint of caramel, and a bit of bitterness.\tNo defect, drinkable, not bad.", "user/profileName": "stcules", "review/aroma": 2.5}
]
그건 그렇고, 파일 경로 또는 파일 개체가 아닌 JSON 문자열이 예상 obj = json.loads('./test.json')대로 작동 json.loads하지 않습니다. ( loads"load string"의 약자입니다.) json.load대신 원합니다 .
TheSavageTeddy Nov 02 2020 at 06:00
파일이 json이 아닙니다. 이것은 여기 전에 요청되었습니다
JSON 작은 따옴표 '를 사용하지 않고 대신 큰 따옴표를 사용합니다."