【发布时间】:2017-08-27 20:17:17
【问题描述】:
我刚开始尝试python,现在我有点进退两难。
我正在尝试从 json 文档中打印,但我只获取数组中的最后一个元素。
[{
"FullMeasure": "1/2 cup",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 1142,
"IngredientName": "frozen corn",
"IngredientStep": 10,
"LanguageId": 0,
"PostPreparation": ", thawed, drained",
"PrePreparation": "",
"QuantityNum": 0,
"QuantityText": "1/2",
"QuantityUnit": "cup",
"RecipeIngredientID": 6291555,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "1/4 cup",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 1523,
"IngredientName": "red pepper",
"IngredientStep": 20,
"LanguageId": 0,
"PostPreparation": "s",
"PrePreparation": "chopped",
"QuantityNum": 0,
"QuantityText": "1/4",
"QuantityUnit": "cup",
"RecipeIngredientID": 6291554,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "2 Tbsp.",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 20197,
"IngredientName": "chopped green chiles",
"IngredientStep": 30,
"LanguageId": 0,
"PostPreparation": ", drained",
"PrePreparation": "canned ",
"QuantityNum": 2,
"QuantityText": "2",
"QuantityUnit": "Tbsp.",
"RecipeIngredientID": 6291552,
"TrialMeasure": "",
"TrialWeight": ""
},
{
"FullMeasure": "",
"FullWeight": "",
"IngredientGridHeaders": null,
"IngredientID": 19682,
"IngredientName": "KRAFT DELI DELUXE Process American Cheese Slice",
"IngredientStep": 80,
"LanguageId": 0,
"PostPreparation": "s",
"PrePreparation": "",
"QuantityNum": 4,
"QuantityText": "4",
"QuantityUnit": "",
"RecipeIngredientID": 6291558,
"TrialMeasure": "",
"TrialWeight": ""
}
]
我想获取所有的成分ID,所以我写了这段小代码来获取成分ID
rec = recipes['Recipes'][0]['Ingredients']
for records in rec:
value = {
'Ingredient ID': records['IngredientID']
}
当我返回值时,我得到了
{
"Ingreient ID": 19682
}
我正在尝试获取每个元素的成分 ID,但我似乎无法弄清楚。任何指针将不胜感激。谢谢
【问题讨论】:
-
如果你能说出你期望的数据格式会很有帮助。
-
你确定这是一个解析的python列表吗?因为没有定义变量“null”?我想你必须先解析它 json 否则你会得到一个错误