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

时间:2018-12-27 11:13:36

标签: java spring resttemplate

我正在尝试从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
}

1 个答案:

答案 0 :(得分:0)

上面的JSON是JSONObject的JSONArray,我相信实际格式是

if __name__ == '__main__':
    rospy.init_node("path_planner")
    app.run(debug=True)

所以这应该映射到包含library(foreign) library(dplyr) mydata = read.dta("http://dss.princeton.edu/training/Panel101.dta") mydata$time = ifelse(mydata$year >= 1994, 1, 0) mydata$treated = ifelse(mydata$country == "E" | mydata$country == "F" | mydata$country == "G", 1, 0) mydata$did = mydata$time * mydata$treated df_0 <- NULL for (i in 1:length(seq(5,nrow(mydata)-1,5))){ index <- seq(5,nrow(mydata),5)[i] df_1 <- NULL for (j in 1:100){ mydata_temp <- mydata[sample(nrow(mydata), index), ] didreg = lm(y ~ treated + time + did, data = mydata_temp) out <- summary(didreg) new_line <- c(out$coefficients[,1][4], out$coefficients[,2][4], index) new_line <- data.frame(t(new_line)) names(new_line) <- c("c","s","i") df_1 <- rbind(df_1,new_line) } df_0 <- rbind(df_0,df_1) } df_0 <- df_0 %>% group_by(i) %>% summarise(c = mean(c, na.rm = T), s = mean(s, na.rm = T)) df_0 <- df_0 %>% group_by(i) %>% mutate(upper = c+s, lower = c-s) df <- df_0 plot(df$i, df$c, ylim=c(min(df_0$c)-5000000000, max(df_0$c)+5000000000), type = "l") polygon(c(df$i,rev(df$i)),c(df$lower,rev(df$upper)),col = "grey75", border = FALSE) lines(df$i, df$c, lwd = 2) 数组的POJO,所以要有包含 { "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" }] } 数组的pojo类

branded

API调用

branded