解析JSON时无法获取Integer/Double类型的quantity值求助
嘿,我来帮你搞定这个问题!你提供的JSON数据里所有quantity字段的数值我已经帮你提取出来啦,同时附上了用Python解析的方法,方便你以后自己操作:
提取JSON中"quantity"字段的数值
方法:使用Python解析JSON
你可以用Python内置的json模块快速解析这段JSON,提取每个食材的quantity值,代码如下:
import json # 你的JSON数据 json_data = '''[ { "id": 1, "name": "Nutella Pie", "ingredients": [ { "quantity": 2, "measure": "CUP", "ingredient": "Graham Cracker crumbs" }, { "quantity": 6, "measure": "TBLSP", "ingredient": "unsalted butter, melted" }, { "quantity": 0.5, "measure": "CUP", "ingredient": "granulated sugar" }, { "quantity": 1.5, "measure": "TSP", "ingredient": "salt" }, { "quantity": 5, "measure": "TBLSP", "ingredient": "vanilla" }, { "quantity": 1, "measure": "K", "ingredient": "Nutella or other chocolate-hazelnut spread" }, { "quantity": 500, "measure": "G", "ingredient": "Mascapone Cheese(room temperature)" }, { "quantity": 1, "measure": "CUP", "ingredient": "heavy cream(cold)" }, { "quantity": 4, "measure": "OZ", "ingredient": "cream cheese(softened)" } ], "steps": [ { "id": 0, "shortDescription": "Recipe Introduction", "description": "Recipe Introduction", "videoURL": "", "thumbnailURL": "" }, { "id": 1, "shortDescription": "Starting prep", "description": "1. Preheat the oven to 350\u00b0F. Butter a 9\" deep dish pie pan.", "videoURL": "", "thumbnailURL": "" }, { "id": 2, "shortDescription": "Prep the cookie crust.", "description": "2. Whisk the graham cracker crumbs, 50 grams (1/4 cup) of sugar, and 1/2 teaspoon of salt together in a medium bowl. Pour the melted butter and 1 teaspoon of vanilla into the dry ingredients and stir together until evenly mixed.", "videoURL": "", "thumbnailURL": "" }, { "id": 3, "shortDescription": "Press the crust into baking form.", "description": "3. Press the cookie crumb mixture into the prepared pie pan and bake for 12 minutes. Let crust cool to room temperature.", "videoURL": "", "thumbnailURL": "" }, { "id": 4, "shortDescription": "Start filling prep", "description": "4. Beat together the nutella, mascarpone, 1 teaspoon of salt, and 1 tablespoon of vanilla on medium speed in a stand mixer or high speed with a hand mixer until fluffy.", "videoURL": "", "thumbnailURL": "" }, { "id": 5, "shortDescription": "Finish filling prep", "description": "5. Beat the cream cheese and 50 grams (1/4 cup) of sugar on medium speed in a stand mixer or high speed with a hand mixer for 3 minutes. Decrease the speed to medium-low and gradually add in the cold cream. Add in 2 teaspoons of vanilla and beat until stiff peaks form.", "videoURL": "", "thumbnailURL": "" }, { "id": 6, "shortDescription": "Finishing Steps", "description": "6. Pour the filling into the prepared crust and smooth the top. Spread the whipped cream over the filling. Refrigerate the pie for at least 2 hours. Then it's ready to serve!", "videoURL": "", "thumbnailURL": "" } ], "servings": 8, "image": "" }, { "id": 2, "name": "Brownies", "ingredients": [ { "quantity": 350, "measure": "G", "ingredient": "Bittersweet chocolate (60-70% cacao)" }, { "quantity": 226, "measure": "G", "ingredient": "unsalted butter" }, { "quantity": 300, "measure": "G", "ingredient": "granulated sugar" }, { "quantity": 100, "measure": "G", "ingredient": "light brown sugar" }, { "quantity": 5, "measure": "UNIT", "ingredient": "large eggs" }, { "quantity": 1, "measure": "TBLSP", "ingredient": "vanilla extract" }, { "quantity": 140, "measure": "G", "ingredient": "all purpose flour" }, { "quantity": 40, "measure": "G", "ingredient": "cocoa powder" }, { "quantity": 1.5, "measure": "TSP", "ingredient": "salt" }, { "quantity": 350, "measure": "G", "ingredient": "semisweet chocolate chips" } ], "steps": [ { "id": 0, "shortDescription": "Recipe Introduction", "description": "Recipe Introduction", "videoURL": "", "thumbnailURL": "" }, { "id": 1, "shortDescription": "Starting prep", "description": "1. Preheat the oven to 350\u00b0F. Butter the bottom and sides of a 9\"x13\" pan.", "videoURL": "", "thumbnailURL": "" }, { "id": 2, "shortDescription": "Melt butter and bittersweet chocolate.", "description": "2. Melt the butter and bittersweet chocolate together in a microwave or a double boiler. If microwaving, heat in 20 second increments, stirring between each until melted." } ] }]''' # 解析JSON data = json.loads(json_data) # 提取所有quantity值 quantity_values = [] for recipe in data: for ingredient in recipe['ingredients']: quantity_values.append((recipe['name'], ingredient['quantity'], type(ingredient['quantity']).__name__)) # 按食谱分类输出 print("按食谱分类的quantity数值:") current_recipe = "" for recipe_name, qty, qty_type in quantity_values: if recipe_name != current_recipe: current_recipe = recipe_name print(f"\n{current_recipe}:") print(f"- {qty} ({qty_type})")
提取结果
Nutella Pie的quantity数值
- 2 (int)
- 6 (int)
- 0.5 (float)
- 1.5 (float)
- 5 (int)
- 1 (int)
- 500 (int)
- 1 (int)
- 4 (int)
Brownies的quantity数值
- 350 (int)
- 226 (int)
- 300 (int)
- 100 (int)
- 5 (int)
- 1 (int)
- 140 (int)
- 40 (int)
- 1.5 (float)
- 350 (int)
内容的提问来源于stack exchange,提问作者Jantzilla
相关产品推荐
相关产品推荐

