gpt4 book ai didi

java - 解析 JSON 时出现错误

转载 作者:行者123 更新时间:2023-12-01 17:50:48 24 4
gpt4 key购买 nike

我正在尝试从 https 请求解析 json,但有一个我找不到的错误。以下是该类的代码,该类将 json 字符串作为输入并返回对象列表。

package com.example.ryzen.bakingapp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class JSONconvert {

private static final String RECIPE_ID = "id";
private static final String RECIPE_NAME = "name";
private static final String INGREDIENTS = "ingredients";
private static final String SERVINGS = "servings";
private static final String IMAGE = "image";
private static final String QUANTITY = "quantity";
private static final String MEASURE = "measure";
private static final String INGREDIENT = "ingredient";
private static final String STEP = "steps";
private static final String STEP_ID = "id";
private static final String SHORT_DESCRIPTION = "shortDescription";
private static final String DESCRIPTION = "description";
private static final String VIDEO_URL = "videoURL";
private static final String THUMBNAIL = "thumbnailURL";

public static List<Recipe> convert (String json) {

List<Recipe> recipesList = new ArrayList<>();
int recipeId = 0;
String name = "";
int servings = 0;
String image = "";
List<Steps> steps = new ArrayList<>();
String thumbnailURL = "";
String videoURL = "";
String description = "";
String shortDescription = "";
int stepId = 0;
List<Ingredients> ingredients = new ArrayList<>();
float quantity = 0;
String measure = "";
String ingredient = "";

try {

JSONArray baseJsonArray = new JSONArray(json);
for (int i = 0; i < baseJsonArray.length(); i++) {
ingredients.clear();
steps.clear();
JSONObject recipeObject = baseJsonArray.getJSONObject(i);
recipeId = recipeObject.getInt(RECIPE_ID);
name = recipeObject.getString(RECIPE_NAME);
servings = recipeObject.getInt(SERVINGS);
image = recipeObject.getString(IMAGE);
JSONArray ingredientsArray = recipeObject.getJSONArray(INGREDIENTS);
for (int j = 0; j < ingredientsArray.length(); j++) {
JSONObject ingredientObject = ingredientsArray.getJSONObject(j);
quantity = ingredientObject.getInt(QUANTITY);
measure = ingredientObject.getString(MEASURE);
ingredient = ingredientObject.getString(INGREDIENT);
ingredients.add(new Ingredients(quantity,measure,ingredient));
}
JSONArray stepArray = recipeObject.getJSONArray(STEP);
for (int k = 0; k < stepArray.length(); k++) {
JSONObject stepsObject = stepArray.getJSONObject(k);
stepId = stepsObject.getInt(STEP_ID);
shortDescription = stepsObject.getString(SHORT_DESCRIPTION);
description = stepsObject.getString(DESCRIPTION);
videoURL = stepsObject.getString(VIDEO_URL);
thumbnailURL = stepsObject.getString(THUMBNAIL);
steps.add(new Steps(stepId, shortDescription,description,videoURL,thumbnailURL));
}
recipesList.add(new Recipe(recipeId, name, servings, image, ingredients, steps));
}
} catch (JSONException e) {

e.printStackTrace();
}
return recipesList; }

This是我正在尝试解析的 json 文件。因此,根据上述内容,我的配料表应包含以下内容:

Graham Cracker crumbs
unsalted butter, melted
granulated sugar
salt
vanilla
Nutella or other chocolate-hazelnut spread
Mascapone Cheese(room temperature)
heavy cream(cold)
cream cheese(softened)
Bittersweet chocolate (60-70% cacao)
unsalted butter
granulated sugar
light brown sugar
large eggs
vanilla extract
all purpose flour
cocoa powder
salt
semisweet chocolate chips
sifted cake flour
granulated sugar
baking powder
salt
vanilla extract, divided
egg yolks
whole milk
unsalted butter, softened and cut into 1 in. cubes
egg whites
melted and cooled bittersweet or semisweet chocolate
Graham Cracker crumbs
unsalted butter, melted
granulated sugar
salt
vanilla,divided
cream cheese, softened
large whole eggs
large egg yolks
heavy cream

但是我得到了这个成分列表

Graham Cracker crumbs
unsalted butter, melted
granulated sugar
salt
vanilla,divided
cream cheese, softened
large whole eggs
large egg yolks
heavy cream
Graham Cracker crumbs
unsalted butter, melted
granulated sugar
salt
vanilla,divided
cream cheese, softened
large whole eggs
large egg yolks
heavy cream
Graham Cracker crumbs
unsalted butter, melted
granulated sugar
salt
vanilla,divided
cream cheese, softened
large whole eggs
large egg yolks
heavy cream
Graham Cracker crumbs
unsalted butter, melted
granulated sugar
salt
vanilla,divided
cream cheese, softened
large whole eggs
large egg yolks
heavy cream

似乎第一个 Ingredients 对象每次都会重复并添加到 Ingredients 列表中。但我不明白为什么会发生这种情况。你看到我缺少的东西了吗?感谢您提前抽出时间。

最佳答案

每次解析新食谱时,您似乎都使用相同的列表来添加成分。由于您使用的是配料.clear(),这意味着每次您创建新的食谱对象时,对列表“配料”的引用/指针保持不变。

当您在解析后列出不同食谱中的成分时,它们都是相同的,因为它们都访问相同的列表,当然仅包含您找到的最后成分。

您应该做的是为每个食谱创建一个新列表,而不是重复使用相同的列表:)

关于java - 解析 JSON 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50379404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com