当前位置: 首页 > 知识库问答 >
问题:

应为BEGIN_OBJECT,但在RETERFIT2中为

涂羽
2023-03-14
{
  "ingredients": [
    {
      "quantity": 1,
      "measureURI": measureUri,
      "foodId": foodId
    }
  ]
}
import requests

APP_ID = "app_id_"
API_KEY = "api_key"
BASE = "https://api.edamam.com/api/food-database/v2/nutrients?"

url = f"https://api.edamam.com/api/food-database/v2/nutrients?app_id={APP_ID}&app_key={API_KEY}"
data = {
        "ingredients": [
            {
                "quantity": 1,
                "measureURI":  "http://www.edamam.com/ontologies/edamam.owl#Measure_unit",
                "foodId":  "food_a1gb9ubb72c7snbuxr3weagwv0dd"
            }
        ]
    }

res = requests.post(url, json=data)
print(res.text) 
@POST(Constants.API_PATH_NUTRIENTS + "?app_id=" + Constants.APP_ID + "&app_key=" + Constants.API_KEY)
Call<NutrientsResponseSchema> getFoodNutrients(@Body NutrientsRequestSchema requestSchema);

和请求模式模型

public class NutrientsRequestSchema {
    public List<IngredientsRequestSchema> ingredients;

    public NutrientsRequestSchema(List<IngredientsRequestSchema> ingredients) {
        this.ingredients = ingredients;
    }
}
public class IngredientsRequestSchema {
    public float quantity;
    public String foodId;

    @SerializedName("measureURI")
    public String measureUri;

    public IngredientsRequestSchema(float quantity,
                                    String measureUri,
                                    String foodId) {
        this.quantity = quantity;
        this.measureUri = measureUri;
        this.foodId = foodId;
    }
}

当我运行代码并从服务中请求时,我得到

com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was
BEGIN_ARRAY at line 298 column 20 path $.ingredients 

共有1个答案

龚安民
2023-03-14

错误消息很清楚,您已经定义了nutrientsresponseschema,因此,您希望freatings是一个对象,但from server的freatings肯定是一个数组,正如

{
  "ingredients": [
    {
      "quantity": 1,
      "measureURI": measureUri,
      "foodId": foodId
    }
  ]
}

成分成分的数组。但是在nutrientsresponsechema中,必须定义了

   @SerializedName("ingredients")
    public Ingredient ingredient; //POJO and not a list of POJO

您可以通过将NutrientsResponseChema更改为

   @SerializedName("ingredients")
    public List<Ingredient> ingredients; //List of POJO

编辑:详细说明:

您有NutrientsResponseSchema:

public class NutrientsResponseSchema {
    public String uri;
    public float calories;
    public float totalWeight;
    public List<String> dietLabels;
    public List<String> healthLabels;
    public List<String> cautions;
 
    public TotalNutrients totalNutrients;
    public Ingredients ingredients;
}

您需要将最后一行更改为:

public class NutrientsResponseSchema {
    public String uri;
    public float calories;
    public float totalWeight;
    public List<String> dietLabels;
    public List<String> healthLabels;
    public List<String> cautions;
 
    public TotalNutrients totalNutrients;
    @SerializedName("ingredients")
    public List<Ingredient> ingredients;
}
public class Ingredient{
    public float quantity;
    public String food;
    public String foodId;
    public float weight;
    public float retainedWeight;
    public String measureUri;
    public String status;
}
 类似资料: