【问题标题】:JSONDecodeError while loading json file [duplicate]加载 json 文件时出现 JSONDecodeError [重复]
【发布时间】:2021-02-14 19:25:44
【问题描述】:

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

我怎样才能正确地做到这一点?非常感谢。

【问题讨论】:

标签: python json python-3.x


【解决方案1】:

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
}] 

【讨论】:

  • 谢谢,有没有办法快速将实际数据转换成你的格式?
  • @ahbon:您可以转到jsonlint.com 并验证您的 JSON。这也将 JSON 转换为缩进。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    问题是您的文件不是有效的 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

    【讨论】:

    • 如何将文件转换为标准 json 格式?真实数据非常大,有 50000 个条目。
    • 顺便说一句,我收到一个错误 AttributeError: 'str' object has no attribute 'read'json.load
    • @ahbon 你有两个选择。 1)找到文件保存为什么格式,并找到一个读取该格式的工具。 2) 使用脚本将文件转换为有效的 JSON。
    • @ahbon AttributeError 听起来您正试图对字符串而不是文件对象使用read() 方法。
    【解决方案4】:

    文件不是 json。这在here之前已经问过了

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

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2018-04-25
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      相关资源
      最近更新 更多