Json映射非标准接口

问题描述 投票:0回答:2

我得到的响应是一个对象映射,但是它们都有相同的键:

{
    "hits": [
    {
      "recipe": {
          "id" : "0",
          "label" : "chicken noodles",
          ...
      },
      "another_field": "value"
    },
     {
      "recipe": {
          "id" : "1",
          "label" : "fried chicken",
          ...
      },
      "another_field": "value"
    },
     { "recipe": {...}, ... }
    ]
}

我的Pojo:

public class SearchRecipeResponse {

    private List<RecipeResponse> hits;

    public List<RecipeResponse> getHits() {
        return hits;
    }
}
public class RecipeResponse{
    private String label;
    ...
    getters, etc
}

结果显示为对象列表,但所有对象均为null,因为json有效负载具有此键“ recipe”。

java jackson restful-url
2个回答
0
投票

您可以为配方创建POJO并将其用于映射。不确定这是您要执行的操作,您需要扩展问题以提高清晰度。


0
投票

我通过向每个对象添加一个额外的映射来进行排序。我的映射是错误的。谢谢您的回答。

public class SearchRecipeResponse {

    private List<HitResponse> hits;

    public List<HitResponse> getHits() {
        return hits;
    }
}
public class HitResponse{
    private RecipeResponse recipe;
    ...
    getters, etc
}

对于那些缺少杰克逊注释的人,我不使用它们,因为字段名称与属性匹配。

© www.soinside.com 2019 - 2024. All rights reserved.