简体   繁体   中英

Json mapping non standard interface

I got a response which is a map of objects but they all have the same key:

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

My Pojo:

public class SearchRecipeResponse {

    private List<RecipeResponse> hits;

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

The result comes up as a list of objects but all objects are null because the json payload has this key "recipe".

You can create an POJO for the recipe and use that for the mapping. Not sure though that's what you want to do, you need to expand your question to add clarity.

I got it sorted by adding an extra mapping to each object. My mapping was wrong. Thank you for the answers.

public class SearchRecipeResponse {

    private List<HitResponse> hits;

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

For those missing the jackson annotations I am not using them as the fields names match the properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM