0

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

我想映射每周时间序列的 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;
}

weekService 为:

@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 中提取的。但是,这里的标签是可能不固定的日期。

4

0 回答 0