{
"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
错误消息很清楚,您已经定义了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;
}
我已经读过许多关于同一错误的答案,但没有任何帮助。 提前道谢。
我是JSON解析方面的新手,并试图解析以下JSON: 我只需要在“FeaturedMedia”中获得链接,所以我只包括了模型中的那些。我也从这里得到了一些关于错误的想法,但错误仍然存在。 任何关于如何解决这个问题的建议都会有很大的帮助。
我不知道该修好它 我怎么了? 我的json是 E/DDD:onFailure:java.lang.IllegalStateException:应为BEGIN_ARRAY,但在第1行第44列路径$.data处为BEGIN_OBJECT 有什么问题?谢谢!
我是android编程的新手,有人能帮助我或者指出为什么它会给我这个错误吗?我想从服务器上获取一些数据,比如硬件json下的数据,并获得名称和状态,但当我调用api时,它向我展示了这一点。
我正在尝试使用改版在我的应用程序上实现登录,但是我一直得到这个错误不确定是什么错误,java.lang.IllegalStateException:应该是BEGIN_OBJECT,但应该是BEGIN_ARRAY 这是邮递员的回复 和我的登录类