Skip to content
Advertisement

Get object from .readEntity

I have a java service which calls an API and gets the following in return

JavaScript

How can i use .readEntity (or something else) to get the object for a given field; say service2

Or just convert all objects inside status to a List<> of objects

String output = response.readEntity(String.class); works, but i dont know how to parse it properly to something useful as i dont want to search in the string

JavaScript

Advertisement

Answer

You can model the data with a POJO and use that to extract the data. A model of the JSON that you’ve posted might look something like

JavaScript

The getters and setters should use JavaBean naming convention. Then make sure you have a JSON provider such as Jackson

JavaScript

Now you can simply pass the POJO class to the readEntity() method and it will deserialize the JSON into your POJO class.

JavaScript

For more details on modeling your JSON, you can do a Google search for “data binding with Jackson”

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement