0

I am trying to consume an API whose response is like:-

{
    "data": [
        [
            "2075/76",
            "2075-2094767",
            "2075/08/29",
            1466,
            "210003502",
            null,
            "गीता  श्रीस काउचा थापा",
            2418146,
            null,
            14224,
            "Production",
            1800,
            0,
            "2074/75",
            "2075/09/02",
            "Cash",
            null,
            "1",
            null,
            null,
            11019,
            "2018/12/17",
            0,
            "T",
            null,
            "2018/12/17"
        ],
        [
            "2075/76",
            "2075-2093892",
            "2075/08/28",
            1466,
            "210003502",
            null,
            "हेमन्त  बुढाथोकी",
            2417027,
            null,
            14224,
            "Production",
            1400,
            0,
            "2074/75",
            "2075/09/02",
            "Cash",
            null,
            "1",
            null,
            null,
            11019,
            "2018/12/17",
            0,
            "T",
            null,
            "2018/12/17"
        ]
]
}

I am using spring boot and rest template like this.

 @GetMapping("/consume")
    public void consumeApi(@RequestParam  String date) throws IOException {
        BankVoucherParams params = setVoucherParams(date);
        RestTemplate restTemplate = new RestTemplate();

        String token = getBearerToken();

        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);


        HttpEntity<BankVoucherParams> request = new HttpEntity<BankVoucherParams>(params, headers);

        String response = restTemplate.postForObject(endpoint, request, String.class);

        ObjectMapper objectMapper = new ObjectMapper();

        Object o = objectMapper.readValue(response,Object.class);




        System.out.println(response);
    }

in the above code I get the response in string form. I want to map every value to a pojo class and insert it into my database. For now I don't know how to map each of these array into my pojo class.

2

2 Answers 2

0

You can convert the json string to a HashMap with key as "data" and value as

List<List<String>> 

or

String[][]. 

Then iterate through the value and set it to a POJO.

If you would like to dynamically assign each position to a certain field of a POJO, you may wan to keep a mapping of position-field and uses reflection to set the fields. http://tutorials.jenkov.com/java-reflection/fields.html

Sign up to request clarification or add additional context in comments.

Comments

0

If the pojo is available in that project scope you can simly parse the whole object like this:

ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class);

where Object can be replaced by your pojo. So you would have something similar to

ResponseEntity<YourClass> response = restTemplate.exchange(url, HttpMethod.GET, entity, YourClass.class);

If you need to map it you can use libraries like Gson or Jackson.

EDIT: As mentioned below you do not receive key/value pairs. Because of that you need to use a mapper library like Gson or Jackson

3 Comments

I don't think it would work, because response does not have any key-value pair, but rather an object array
how do i map with gson or jackson ?
@sagarlimbu, I don't think you would be able to. Refer to the link mentioned in my comment to your question. You would need to have a similar implementation.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.