加载json文件时出现JSONDecodeError

时间:2020-11-02 05:54:59

标签: python json python-3.x

给出一个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)

我该怎么做呢?提前表示感谢。

4 个答案:

答案 0 :(得分:2)

JSON中有2个问题。

  1. 缺少数组括号[]
  2. '修改为"

正确的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 :(得分:1)

这不是json文件。看起来像ndjson 查看第三方软件包ndjson

此外,它应该使用双引号而不是单引号。

如果这是您创建或可以更改的文件-您可以将这3个JSON对象放入JSON数组中,然后t将是有效的JSON。

答案 2 :(得分:1)

问题是您的文件不是有效的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是“加载字符串”的缩写。)您想使用json.load

答案 3 :(得分:0)

该文件不是json。在here

之前已被问过

JSON不使用单引号',而是使用双引号"