简体   繁体   English

如何从Spring Boot中嵌套的JSON数据中提取值

[英]How to extract value from nested JSON data in Spring Boot

Currently, I am calling an API every 3 minutes to get weather data.目前,我每 3 分钟拨打一次 API 以获取天气数据。 This is the response I get as a String:这是我作为字符串得到的响应:


{
    "data": {
        "timelines": [
            {
                "timestep": "current",
                "endTime": "2023-01-20T13:33:00-05:00",
                "startTime": "2023-01-20T13:33:00-05:00",
                "intervals": [
                    {
                        "startTime": "2023-01-20T13:33:00-05:00",
                        "values": {
                            "dewPoint": 18.63,
                            "humidity": 66,
                            "precipitationIntensity": 0,
                            "precipitationProbability": 0,
                            "pressureSeaLevel": 1019.19,
                            "pressureSurfaceLevel": 1018.65,
                            "temperature": 25.5,
                            "temperatureApparent": 25.5,
                            "uvIndex": 3,
                            "windDirection": 151.69,
                            "windSpeed": 4.88
                        }
                    }
                ]
            }
        ]
    }
}

I would like to know how to extract the value of the attributes in "values" (like "dewPoint", "humidity", etc.).我想知道如何提取“值”中的属性值(如“露点”、“湿度”等)。

I've been trying to do this with Beans and such, but the fact that the data is nested and some of it is an array is making it difficult for me to understand.我一直在尝试使用 Beans 等来做到这一点,但是数据是嵌套的并且其中一些是数组这一事实让我很难理解。 Basically, I want to extract each of the values of the attributes in "values" from a JSON String, concat them in to an array of only the values, and then send that array to be saved in a database.基本上,我想从 JSON 字符串中提取“值”中属性的每个值,将它们连接成一个仅包含值的数组,然后将该数组保存在数据库中。

You will first need to create the classes to model this and then take the string and use an Object mapper to take a string and convert it to an object.您首先需要为 model 创建类,然后获取字符串并使用Object 映射器获取字符串并将其转换为 object。

class WeatherData {
    List<Timelines> timelines; // all other object are nested in here.
}

class Timelines {
    String timestep;
    String endTime;
    String startTime;
    List<Intervals> intervals;
}

class Intervals {
    String startTime;
    Values values;
}

class Values {
  double dewPoint;
  int humidity;
  int precipitationIntensity;
  int precipitationProbability;
  double pressureSeaLevel;
  double pressureSurfaceLevel;
  double temperature;
  double temperatureApparent;
  int uvIndex;
  double windDirection; 
  double windSpeed;
}
WeatherData data = new ObjectMapper().readValue(jsonString, WeatherData.class);

There are several ways to approach this once you map the json to an Object. You can loop through the values, so two for loops.一旦将 map json 转换为 Object,有几种方法可以解决这个问题。您可以遍历这些值,所以有两个 for 循环。 Once you get to the intervals you can add the values to the array.到达间隔后,您可以将值添加到数组中。 List<WeatherData> newWeatherDataList = new ArrayList();

The other way would be to flatten down the object so it's not so nested.另一种方法是将 object 压平,这样它就不会那么嵌套了。 I'd suggest looking up how to flatten an object in Java .我建议查找如何在 Java 中压平 object You will still need one for loop to loop through the intervals array.您仍然需要一个 for 循环来遍历间隔数组。 Then just add it to the new list like above.然后像上面一样将它添加到新列表中。

From here you can batch-insert the data into a table .从这里您可以将数据批量插入到表中

I hope this helps let me know if I can update the answer to provide more context.我希望这有助于让我知道我是否可以更新答案以提供更多背景信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM