I'm calling API and getting the response as a JSON array I need to map that array into Java object array. Here I've defined the model class below you can see API JSON array, Model class and the way I try to map JSON array into Java object array. I need to make sure if this approach is correct or not.
API response JSON array
[
{
"sanctionId": 594,
"listId": "SDN",
"listType": "SANCTION",
"publisher": "OFAC",
"addedDate": "2022-02-11T04:52:59.775+0000",
"programs": "CUBA",
"name": " CUBA CIGARS TRADE",
"title": null,
"type": "Entity",
"contacts": [
{
"id": 361,
"address": " ",
"city": "",
"state_province": "",
"country": "Italy",
"remarks": null
}
],
"identities": null,
"remarks": null,
"other": null,
"score": 2.1659167
},
{
"sanctionId": 602,
"listId": "SDN",
"listType": "SANCTION",
"publisher": "OFAC",
"addedDate": "2022-02-11T04:52:59.779+0000",
"programs": "CUBA",
"name": " CUBA N CIGARS TRADE",
"title": null,
"type": "Entity",
"contacts": [
{
"id": 361,
"address": " ",
"city": "",
"state_province": "",
"country": "Italy",
"remarks": null
}
],
"identities": null,
"remarks": null,
"other": null,
"score": 1.9129416
}
]
Model Class(here I've omitted getters and setters) :
public class SanctionInquiryResponse {
private String sanctionId="";
private String listId="";
private String listType="";
private String publisher="";
private String addedDate="";
private String programs="";
private String name="";
private String title="";
private String type="";
private String identities="";
private String remarks="";
private String other="";
private String score="";
private List<Contacts> contacts = null;
}
public class Contacts {
private String id = "";
private String address = "";
private String city = "";
private String state_Province = "";
private String country = "";
private String remarks = "";
}
Here is the way I'm trying to map the JSON array into the Java object array
String url="http://localhost:8070/getObject";
ObjectMapper mapper = new ObjectMapper();
//String jsonInString = mapper.writeValueAsString(openCaseRequest);
Client client = Client.create();
client.setConnectTimeout(120*1000);
client.setReadTimeout(120*1000);
WebResource webResource = client.resource(url);
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class);
String output = response.getEntity(String.class);
JSONArray jsonArray = new JSONArray(output);
Gson gson = new Gson();
for (int i = 0; i < jsonArray.length(); i++) {
sanctionInquiryResponse = gson.fromJson(jsonArray.getJSONObject(i).toString() ,SanctionInquiryResponse.class);
}