Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Rest模板从外部api返回特定数据_Java_Spring_Spring Boot - Fatal编程技术网

Java 使用Rest模板从外部api返回特定数据

Java 使用Rest模板从外部api返回特定数据,java,spring,spring-boot,Java,Spring,Spring Boot,当使用RestTemplate调用外部api时,我有点卡住了,因为我的请求返回了一个字符串。我来自js背景,所以使用js我只需返回json并提取我需要的内容 我只想获取当天(2020-02-07)的股票信息,不需要元数据等 我的代码: RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject(uri, String.class); return result; 代码返回以

当使用RestTemplate调用外部api时,我有点卡住了,因为我的请求返回了一个字符串。我来自js背景,所以使用js我只需返回json并提取我需要的内容

我只想获取当天(2020-02-07)的股票信息,不需要元数据等

我的代码:

RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
return result; 
代码返回以下内容:

    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "XYL",
        "3. Last Refreshed": "2020-02-07",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2020-02-07": {
            "1. open": "83.3600",
            "2. high": "84.9100",
            "3. low": "83.1300",
            "4. close": "84.2000",
            "5. volume": "2495921"
        },
        "2020-02-06": {
            "1. open": "81.0000",
            "2. high": "83.3100",
            "3. low": "80.8000",
            "4. close": "83.1200",
            "5. volume": "5052421"
        }
我只希望我的请求返回当前日期:

"2020-02-07": {
            "1. open": "83.3600",
            "2. high": "84.9100",
            "3. low": "83.1300",
            "4. close": "84.2000",
            "5. volume": "2495921"
        }

非常感谢您的帮助

您需要创建一个pojo类,以便使用返回的JSON并将其映射到Java实例中

例如,如果您有以下内容:

public class TimeSeriesDaily{
    @JsonProperty("Time Series (Daily)")
    private Map<String, Map<String, String>> dailyValues; 
}
此参考也可能有用:

TimeSeriesDaily result = restTemplate.getForObject(uri, TimeSeriesDaily.class);