我有數據這樣的JSON文件:如何從json文件讀取數據?
{
"id": 1008,
"description": "Cheese, caraway",
"tags": [ ],
"manufacturer": "",
"group": "Dairy and Egg Products",
"portions": [
{
"amount": 1,
"unit": "oz",
"grams": 28.35
}
],
"nutrients": [
{
"value": 25.18,
"units": "g",
"description": "Protein",
"group": "Composition"
},
{
"value": 29.2,
"units": "g",
"description": "Total lipid (fat)",
"group": "Composition"
},
{
"value": 3.06,
"units": "g",
"description": "Carbohydrate, by difference",
"group": "Composition"
},
{
"value": 3.28,
"units": "g",
"description": "Ash",
"group": "Other"
}
]
}
和我用下面的代碼,試圖從中讀取數據,
import json
path = 'C:\\Users\\IBM_ADMIN\\Desktop\\ml-1m\\food_nutrients_database - 副本.json'
data = open(path).read()
records = json.loads(data)
但出現以下錯誤:
records = json.loads(data)
ValueError: Expecting value: line 1 column 1 (char 0)
這裏有什麼問題?我注意到從「數據」返回的結果以「'鍩begin」開頭,這是可能的原因嗎?如果是這樣,如何解決它?
顯然你的JSON格式不正確混淆。 –