java - 如何修复无法从 START_OBJECT 令牌中反序列化 Object[] 的实例
问题描述
我正在尝试从 RestTemplate 检索对象,但每次发生 JsonMappingException 时。那是我试图在其上映射 json 对象的类吗?看起来一开始的数组可能是个问题,但我无法弄清楚。
Caused by: com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of com.lubaszak.bean.ProductInfo[] out
of START_OBJECT token
at [Source: java.io.PushbackInputStream@10a3c667; line: 1, column: 1]
public ResponseEntity<ProductInfo[]> getProductByQuery(@PathVariable String query) {
HttpEntity<?> httpEntity = headersProvider.getHeaders();
ResponseEntity<ProductInfo[]> product = restConfig.createRestTemplate()
.exchange("https://trackapi.nutritionix.com/v2/search/instant?query={query}&common=false&branded=false",HttpMethod.GET, httpEntity, ProductInfo[].class, query);
return product;
json对象:
"branded": [
{
"food_name": "Big Mac",
"serving_unit": "burger",
"nix_brand_id": "513fbc1283aa2dc80c000053",
"brand_name_item_name": "McDonald's Big Mac",
"serving_qty": 1,
"nf_calories": 540,
"photo": {
"thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
"highres": null,
"is_user_uploaded": false
},
"brand_name": "McDonald's",
"region": 1,
"brand_type": 1,
"nix_item_id": "513fc9e73fe3ffd40300109f",
"locale": "en_US"
},
Java类:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductInfo {
@JsonProperty("food_name")
public String foodName;
@JsonProperty("serving_unit")
public String servingUnit;
@JsonProperty("nix_brand_id")
public String nixBrandId;
@JsonProperty("brand_name_item_name")
public String brandNameItemName;
@JsonProperty("serving_qty")
public Integer servingQty;
@JsonProperty("nf_calories")
public Integer nfCalories;
@JsonProperty("brand_name")
public String brandName;
@JsonProperty("brand_type")
public Integer brandType;
@JsonProperty("nix_item_id")
public String nixItemId;
//getter methods
}
解决方案
上面的 JSON 是 JSONObject 的 JSONArray,我相信实际格式是
{
"branded": [
{
"food_name": "Big Mac",
"serving_unit": "burger",
"nix_brand_id": "513fbc1283aa2dc80c000053",
"brand_name_item_name": "McDonald's Big Mac",
"serving_qty": 1,
"nf_calories": 540,
"photo": {
"thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
"highres": null,
"is_user_uploaded": false
},
"brand_name": "McDonald's",
"region": 1,
"brand_type": 1,
"nix_item_id": "513fc9e73fe3ffd40300109f",
"locale": "en_US"
},
{
"food_name": "Big Mac",
"serving_unit": "burger",
"nix_brand_id": "513fbc1283aa2dc80c000053",
"brand_name_item_name": "McDonald's Big Mac",
"serving_qty": 1,
"nf_calories": 540,
"photo": {
"thumb": "https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png",
"highres": null,
"is_user_uploaded": false
},
"brand_name": "McDonald's",
"region": 1,
"brand_type": 1,
"nix_item_id": "513fc9e73fe3ffd40300109f",
"locale": "en_US"
}]
}
所以这应该映射到包含branded
数组的 POJO,所以有由branded
数组组成的 pojo 类
public class ProductResponse{
@JsonProperty("branded")
private ProductInfo[] branded;
//getters and setters
}
API 调用
ResponseEntity<ProductResponse> product = restConfig.createRestTemplate()
.exchange("https://trackapi.nutritionix.com/v2/search/instant?query={query}&common=false&branded=false",HttpMethod.GET, httpEntity, ProductResponse.class, query);
return product;
推荐阅读
- python - 返回索引的递归二分查找
- reactjs - React 和 TypeScript:避免使用上下文默认值
- firebase - Flutter:Firebase 实时数据库 orderByChild 对查询结果没有影响
- javascript - ExtJS 6.0.2:如何强制自定义进度条网格列在排序时更新
- python - 如何从输入的值中列出一个列表?
- sqlite - 为什么只执行 delete 语句?
- ruby - RVM 安装 Ruby 错误“文件名太长”
- javascript - 正确加载 Googe API 后未创建 gapi.client
- r - 在 R Shiny 中检测到 selectInput 值更改为 NULL
- exception - 从redis缓存反序列化java.time.Instant期间出现异常