0

I need to unmarshall JSON having array as a top-level element:

[
    {
        "test1": 
        {
            "name": "Boost",
            "fraction": 0.55
        }
    },
    {
        "test2": 
        {
            "name": "Boost",
            "fraction": 0.55
        }
    }
]

My top-level element looks like this:

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

public class Wrapper<T> {

    @XmlTransient
    private List<T> items;

    public Wrapper() {
        items = new ArrayList<>();
    }

    public Wrapper(List<T> items) {
        this.items = items;
    }

    @XmlElement
    public List<T> getItems() {
        return items;
    }

}

Unmarshalling is done in this method:

private <T> T get(String path, Class<T> aClass) {
    WebTarget target = root.path(path);
    Invocation.Builder request = target.request(MediaType.APPLICATION_JSON_TYPE);
    return request.get(aClass);
}

When I tried to marshall an existing object into json I have got the following result:

    {
   "items" : [
        {
            "test1": 
            {
                "name": "Boost",
                "fraction": 0.55
            }
        },
        {
            "test2": 
            {
                "name": "Boost",
                "fraction": 0.55
            }
        }
    ]}

How can I force JAXB to skip this items key?

Thanks for your help.

1 Answer 1

0

I solved the problem using GenericType new GenericType<List<MyObject>>() {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.