JSONDecodeError ขณะโหลดไฟล์ json [ซ้ำ]
ให้ไฟล์ json ที่test.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 ประเด็นใน 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
}]
นี่ไม่ใช่ไฟล์ json ดูเหมือนว่าndjson จะดูแพ็คเกจของบุคคลที่สามndjson
นอกจากนี้ควรใช้เครื่องหมายคำพูดคู่ไม่ใช่คำพูดเดี่ยว
หากเป็นไฟล์ที่คุณสร้างหรือเปลี่ยนแปลงได้คุณสามารถใส่ออบเจ็กต์ JSON ทั้ง 3 นี้ลงในอาร์เรย์ JSON จากนั้น t จะเป็น 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}
]
อย่างไรก็ตามobj = json.loads('./test.json')จะไม่ทำงานตามที่json.loadsคาดไว้สตริง JSON ไม่ใช่เส้นทางไฟล์หรือออบเจ็กต์ไฟล์ ( loadsย่อมาจาก "load string") คุณต้องการjson.loadแทน
ไฟล์ไม่ใช่ json นี่เคยถามมาก่อนที่นี่
JSON ไม่'ใช้เครื่องหมายคำพูดเดี่ยว แต่ใช้เครื่องหมายคำพูดคู่แทน"