简体   繁体   中英

How to parse Json Array from REST post request

how can I parse JSON array to list with objects. Client from another system from which I call request is consuming in body JSON and returning in response

List<Model1>

as JSON. When I call this request I want to get this response and convert it to List (which is in my system). I know the structure of objects included in list, so I prepared POJO class to parse model1 into model2. Do I need to wrap my Model2 class (POJO in my system) to another class?

I'm using WebTarget, URL is completed and it looks like this:

WebTarget wt = target();

 wt.path(URL)
.request()
.accept(MediaType.APPLICATION_JSON)
.post(Entity.entity(consumedBody,MediaType.APPLICATION_JSON), Model2);

Or should I use wrapped class that contains one field with List, let's call this WrapperModel2

 wt.path(URL)
.request()
.accept(MediaType.APPLICATION_JSON)
.post(Entity.entity(consumedBody,MediaType.APPLICATION_JSON), WrapperModel2);

Each time I got error that says "Couldnt parse JSON Array into: Model2 or WrapperModel2. What's the way to correctly parse Json Array into List of specific objects?

I believe you are using JAX-RS, So could you please try this:

.post(Entity.entity(request, MediaType.APPLICATION_JSON_TYPE), new GenericType<List<Model2>>() {
        });

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