gpt4 book ai didi

java - com.fasterxml.jackson.databind.exc.MismatchedInputException : Cannot deserialize instance of Object out of START_ARRAY token

转载 作者:行者123 更新时间:2023-12-01 16:38:02 26 4
gpt4 key购买 nike

我陷入了 JSON 到 Java 对象之间的映射。我正在使用 RestTemplate 在 Spring Boot 中进行第三方 API 调用。

{
"parsed": [
{
"food": {
"foodId": "<FoodID>",
"label": "apple",
"nutrients": {
"ENERC_KCAL": 52,
"PROCNT": 0.26,
"FAT": 0.17,
"CHOCDF": 13.81,
"FIBTG": 2.4
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "<Image URL>"
}
}
]
}

我已经创建了以下相同的类(有 getter 和 setter)。

@Component
@Scope("prototype")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseFoodDetailsFromAPI implements Serializable{
private static final long serialVersionUID = -1927057943962033354L;
ParsesList parsed;
}
public class ParsesList implements Serializable{
private static final long serialVersionUID = 5902036842725888370L;
List<Parse> parse;
}
public class Parse implements Serializable{
private static final long serialVersionUID = -8651880919902785186L;
private Food food;

}
public class Food implements Serializable{
private static final long serialVersionUID = -5933882633489466961L;
private String foodId;
private String label;
Nutrients NutrientsObject;
private String brand;
private String category;
private String categoryLabel;
private String foodContentsLabel;
private String image;
}
public class Nutrients implements Serializable{
private static final long serialVersionUID = -8207662942326982309L;
private float ENERC_KCAL;
private float PROCNT;
private float FAT;
private float CHOCDF;
private float FIBTG;
}

但是我遇到了异常

[Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting 
response for type [class com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI]
and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Cannot deserialize instance of
`com.vikas.projects.organicecommerce.redisapicall.models.ParsesList`
out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot deserialize instance of `com.vikas.projects.organicecommerce.redisapicall.models.ParsesList` out of START_ARRAY
token at [Source: (PushbackInputStream); line: 3, column: 14] (through reference chain:
com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI["parsed"])]
with root cause

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.vikas.projects.organicecommerce.redisapicall.models.ParsesList` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 3, column: 14] (through reference chain: com.vikas.projects.organicecommerce.redisapicall.models.ResponseFoodDetailsFromAPI["parsed"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1445) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1219) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1171) ~[jackson-databind-2.10.4.jar:2.10.4]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1478)

添加的 JSON 是完整 JSON 的一部分。如果需要完整的 JSON,请告诉我。

最佳答案

根据您共享的 JSON,您需要更正一些内容

@Component
@Scope("prototype")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseFoodDetailsFromAPI implements Serializable{
private static final long serialVersionUID = -1927057943962033354L;
List<Parse> parsed;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Food implements Serializable{
private static final long serialVersionUID = -5933882633489466961L;
private String foodId;
private String label;
Nutrients nutrients;
private String brand;
private String category;
private String categoryLabel;
private String foodContentsLabel;
private String image;
}

关于java - com.fasterxml.jackson.databind.exc.MismatchedInputException : Cannot deserialize instance of Object out of START_ARRAY token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61918759/

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