如何创建将 JSON 响应映射到对象列表的函数

问题描述

我从 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",

我想映射每周时间序列的 JSON 响应。 这样我就有了一个对象列表,其中日期是 ID,打开、关闭等是对象数据变量。

这个要在spring boot中完成

请帮忙解决这个问题

我到处搜索如何做到这一点。 我最接近的是使用 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;
}

周服务如下:

@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);
    }
}

然而,这仅提供对每周调整时间序列的响应,而不是将 JSON 映射到对象

在其他示例中,我发现数据是使用标签从 JSON 中提取的。然而,这里的标签是可能不固定的日期。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)