使用杰克逊提取内部JSON数组列表

时间:2018-11-08 13:25:42

标签: java json rest jackson

我的API需要从下面的JSON返回条目对象列表。我正在使用球衣和杰克逊。理想情况下,我只想创建一个Java类PermissionEnty,并使用我的API从JSON返回PermissionEntry对象的列表。我无法使用以下方法反序列化?有人可以建议出什么问题吗?我添加了UNWRAP_ROOT_VALUE,因此我假定它忽略了“列表”节点,并且我将获得“列表”节点以下的项目。

public class PermissionEntry {

    private String id;
    private String displayName;
    private String memberType;
}

和json;

{
    "list": {
        "pagination": {
            "count": 5,
            "hasMoreItems": false,            
        },
        "entries": [
            {
                "entry": {
                    "displayName": "norma",
                    "id": "norma",
                    "memberType": "PERSON"
                }
            },              
            {
                "entry": {
                    "displayName": "clara",
                    "id": "clara",
                    "memberType": "PERSON"
                }
            },
            {
                "entry": {
                    "displayName": "michael",
                    "id": "mich",
                    "memberType": "PERSON"
                }
            }
        ]
    }
}

PermissionEntries

public class PermissionEntries {
  @JsonProperty(value = "entries")
    @JsonDeserialize(using = PermissionEntryDeserializer.class)
    private List<PermissionEntry> entries;

    public List<PermissionEntry> getEntries() {
        return entries;
    }

    public void setEntries(List<PermissionEntry> entries) {
        this.entries = entries;
    }
}

下面是我正在使用的反序列化器

public class PermissionEntryDeserializer extends JsonDeserializer<List<PermissionEntry>> {

    private static final String ENTRY = "entries";
    private static final ObjectMapper mapper = new ObjectMapper()
            .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
    private static final CollectionType collectionType =
            TypeFactory
                    .defaultInstance()
                    .constructCollectionType(List.class, PermissionEntry.class);

    @Override
    public List<PermissionEntry> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException {

        ObjectNode objectNode = mapper.readTree(jsonParser);
        JsonNode nodeEntries = objectNode.get(ENTRY);

        if (null == nodeEntries                     // if no ENTRY node could be found
                || !nodeEntries.isArray()           // or ENTRY node is not an array
                || !nodeEntries.elements().hasNext())   // or ENTRY node doesn't contain any entry
            return null;

        return mapper.readerFor(collectionType).readValue(nodeEntries);
    }
}

服务API

public Optional<List<PermissionEntry>> getPermissionsForGroup(String groupName) {
            Response response = getTarget()
                    .path("/api/group/" + groupName + "/members")
                    .request()
                    .get();

                PermissionEntries list = response.readEntity(PermissionEntries.class);

}

1 个答案:

答案 0 :(得分:0)

我不明白您在这个问题中的意思是“ 有人可以告诉我我需要创建多少个Java类才能获得条目列表。”,但是您已经有了一个条目称为PermissionEntry的对象。您将拥有该对象的列表

这是您的jersey数据的jakcson客户。

 ClientConfig clientConfig = new DefaultClientConfig();
 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
 Client client = Client.create(clientConfig);

 String URL = "http://{{host}}:{{port}}/entry";
 WebResource webResourceGet = client.resource(URL);
 ClientResponse response = webResourceGet.accept("application/json").get(ClientResponse.class);
 String output = response.getEntity(String.class);
 ResponseList responseList= mapper.readValue(output , ResponseList .class);//here is the permissionEntry list that you wil have 

此外,您应该在Pagination数据中为分页创建一个名称为json的对象。您可以创建另一个包含List<PermissionEntry>Pagination的对象,称为ResponseList