How to create a function to map the JSON response to a list of Objects

99 Views Asked by At

I getting the response from the API

{
    "Meta Data": {
        "1. Information": "Weekly Adjusted Prices and Volumes",
        "2. Symbol": "IBM",
        "3. Last Refreshed": "2021-05-19",
        "4. Time Zone": "US/Eastern"
    },
    "Weekly Adjusted Time Series": {
        "2021-05-19": {
            "1. open": "144.4400",
            "2. high": "145.8000",
            "3. low": "140.9200",
            "4. close": "143.1900",
            "5. adjusted close": "143.1900",
            "6. volume": "12399954",
            "7. dividend amount": "0.0000"
        },
        "2021-05-14": {
            "1. open": "145.8000",
            "2. high": "148.3800",
            "3. low": "141.1400",
            "4. close": "144.6800",
            "5. adjusted close": "144.6800",
            "6. volume": "27415665",
            "7. dividend amount": "0.0000"
        },

I want to map the JSON response of the Weekly time series. in such a way that I have a list of objects where date is the ID and the open, close, etc are the object data variables.

This is to be done in spring boot

Kindly help on this

I have searched everwhere on how this can be done. The closest I have come is using JsonNode

@GetMapping("/tryObject")
public WeeklyTimeSeries getWeeklyTimeSeries() throws IOException
{
    String uri ="https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=IBM&apikey=demo";
    RestTemplate restTemplate = new RestTemplate();
    WeeklyTimeSeries result = restTemplate.getForObject(uri, WeeklyTimeSeries.class);
    String strResult = restTemplate.getForObject(uri, String.class);
        weekService.getJsonObjects(strResult);
    return result;
}

The weekService is as :

@Service
@Component
public class weeklyTimeService {
  public void getJsonObjects(String result) throws IOException
    {
        JsonNode jsonNode = new ObjectMapper().readTree(result);
        weekReport report = new weekReport();

        System.out.println(jsonNode);
    }
}

However this only gives response for the Weekly Adjusted Time Series and not map the JSON to objects

In other examples , i find the data is extracted from JSON by using a tag . However , here the tag is the date which may not be fixed.

0

There are 0 best solutions below