杰克逊直接将嵌套列表作为POJO

时间:2014-09-29 02:09:33

标签: java json jackson

首先,标题可能不是最好的。随意编辑。

问题:假设有这个json(缺少报价,我知道):

{
    meta: {
        code: 200
    },
    response: {
        suggestedFilters: { },
        suggestedRadius: 922,
        headerLocation: "New York",
        headerFullLocation: "New York",
        headerLocationGranularity: "city",
        totalResults: 246,
        groups: [
            {
                type: "Recommended Places",
                name: "recommended",
                items: [
                    {
                        // item I care
                    },
                    {
                        // item I care
                    }
                ]
            }
        ]
    }
}

是否有必要通过POJO中的整个路径?例如,现在我的班级是:

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyVenueResponse {

    private VenueResponse response;
    public VenueResponse getResponse() {
        return response;
    }

    public class VenueResponse{

        private List<VenueGroup> groups;
        public List<VenueGroup> getGroups() {
            return groups;
        }
    }

    public class VenueGroup {

        private ArrayList<GroupItems> items;
        public ArrayList<GroupItems> getItems() {
            return items;
        }

    }

}

我真的不关心所有的中级课程,只关心public ArrayList<GroupItems> getItems()。有没有办法“快捷”过程并告诉杰克逊跳过“响应”并从groups对象开始或不知道如何映射它?

请注意,我使用databind,如:

objectMapper.readValue(body.charStream(), MyVenueResponse.class); // where body is a ResponseBody from OKHttp

1 个答案:

答案 0 :(得分:2)

您可以使用the Jackson Tree API遍历输入JSON直到某个点,然后将子树转换为Java对象。这是一个例子:

public class JacksonNestedList {
    public final static String JSON = "{\n"
            + "    meta: {\n"
            + "        code: 200\n"
            + "    },\n"
            + "    response: {\n"
            + "        suggestedFilters: { },\n"
            + "        suggestedRadius: 922,\n"
            + "        headerLocation: \"New York\",\n"
            + "        headerFullLocation: \"New York\",\n"
            + "        headerLocationGranularity: \"city\",\n"
            + "        totalResults: 246,\n"
            + "        groups: [\n"
            + "            {\n"
            + "                type: \"Recommended Places\",\n"
            + "                name: \"recommended\",\n"
            + "                items: [\n"
            + "                    {\n"
            + "                        key: \"value1\"\n"
            + "                    },\n"
            + "                    {\n"
            + "                        key: \"value2\"\n"
            + "                    }\n"
            + "                ]\n"
            + "            }\n"
            + "        ]\n"
            + "    }\n"
            + "}";

    public static class GroupItem {
        public String key;

        @Override
        public String toString() {
            return "key:" + key;
        }
    }

    public static void main(String[] args) throws IOException {
        final ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

        final JsonParser items = mapper.readTree(JSON)
                .path("response")
                .path("groups")
                .get(0)
                .path("items")
                .traverse();
        System.out.println(mapper.readValue(items, new TypeReference<List<GroupItem>>() {}));
    }
}

输出:

[key:value1, key:value2]