Подскажите пожалуйста как мне достать объекты из JSON(при помощи JSONObject или Array) на Java? и сохранить в базу
Хочу достать объекты из Json weatherbit api , выдает ошибки о ненайденных элементах
String weatherappi = "weatherbit.io";
RequestEntity<?> request = RequestEntity.get(url).accept(MediaType.APPLICATION_JSON).build();
ResponseEntity<T> exchange = this.restTemplate.exchange(request, responseType);
JSONObject obj = new JSONObject(exchange.getBody());
//db save logic
WeatherEntity weatherEntity = new WeatherEntity();
JSONArray jsonArray = obj.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj1 = jsonArray.getJSONObject(i);
String strdate = obj1.getString("ob_time");
Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(strdate);
weatherEntity.setTimestamp(date.toInstant());
weatherEntity.setCity(obj1.getString("city_name"));
weatherEntity.setTemperature(obj1.optInt("temp"));
}
Сам Json
{
"data": [
{
"rh": 45,
"pod": "d",
"lon": 37.61556,
"pres": 978.78,
"timezone": "Europe/Moscow",
"ob_time": "2020-04-25 12:17",
"country_code": "RU",
"clouds": 97,
"ts": 1587817073,
"solar_rad": 160.383,
"state_code": "48",
"city_name": "Moscow",
"wind_spd": 2.11647,
"last_ob_time": "2020-04-25T11:30:00",
"wind_cdir_full": "west",
"wind_cdir": "W",
"slp": 996.168,
"vis": 24.135,
"h_angle": 33.8,
"sunset": "16:58",
"dni": 783.56,
"dewpt": -2.2,
"snow": 0,
"uv": 1.16599,
"precip": 2.21053,
"wind_dir": 274,
"sunrise": "01:57",
"ghi": 495.34,
"dhi": 94.33,
"aqi": 31,
"lat": 55.75222,
"weather": {
"icon": "r01d",
"code": "500",
"description": "Light rain"
},
"datetime": "2020-04-25:12",
"temp": 9.2,
"station": "UUEE",
"elev_angle": 38.44,
"app_temp": 9.2
}
],
"count": 1
}