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

如何修复START_OBJECT令牌无法反序列化Object[]实例

洪浩
2023-03-14
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;
"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"
    },
@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
}

共有1个答案

满勇军
2023-03-14

上面的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;
 类似资料: