本文介绍了Python杰森.仅获取json数组中的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始尝试python,现在我陷入了两难境地.

I just started trying out python, and right now i am in a little bit of a dilemma.

我正在尝试从json文档中打印,而我只获取数组中的最后一个元素.

I am trying to print from a json documents, and i am only getting the last element in the array.

[{
    "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": ""
  }
]

我想获取所有的IngredientID,所以我写了这段小代码来获取IngredientID

I will like to get all the ingredientID, so i wrote this little piece of code to grab the IngredientID

rec = recipes['Recipes'][0]['Ingredients']
        for records in rec:
            value = {
                        'Ingredient ID': records['IngredientID']
                    }

当我返回值时,我得到

{
  "Ingreient ID": 19682
}

我正在尝试获取每个元素的成分ID,但我似乎无法弄清楚.任何指针将不胜感激.谢谢

I am trying to get the ingredient ID of each element and i just can't seem to figure it out. Any pointers will be well appreciated. Thank you

推荐答案

您每次通过该循环都将替换该值.您应该改为添加值.

You are replacing the value every time through that loop. You should be adding to the value, instead.

因此,首先将值创建为一个空列表(在循环之前),然后在循环的每次迭代中,将其追加到该列表:

So first create the value as an empty list (before the loop), then on each iteration of the loop, append to that list:

value = []
rec = recipes['Recipes'][0]['Ingredients']
for records in rec:
    value.append({'Ingredient ID': records['IngredientID']})

但是,有一个字典列表,其中每个字典都有一个具有相同已知键的单个值,这似乎没有意义.根据您的要求,您可能需要执行以下任一操作:

However, having a list of dictionaries, where each dictionary has one single value with the same known key, seems a bit pointless. Depending on your requirements, you probably may want to do either this:

    value.append(rec)

    value.append(records['IngredientID'])

这篇关于Python杰森.仅获取json数组中的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:47