【发布时间】:2018-10-16 13:40:59
【问题描述】:
我想在屏幕上输出一个字符串,比如"Dublin is experiencing clouds today with a temperature of 15".
如何从这个 JSON 中定位 weather.main 和 temperature 并将其放入我的字符串中?
这是我用于定位 api 并返回 JSON 的代码。我是新手,所以任何帮助都将不胜感激!
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
@Path("/weather")
public class Weather {
@GET
@Path("/{param}")
public Response GetCityInfo(@PathParam("param") String city) {
String URL = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&mode=JSON&APPID=cbb5da68f37d26059628449e068ce931";
Client c = ClientBuilder.newClient();
Response r = c.target(URL).request().get();
return r;
}
}
使用带有 URL http://localhost:49000/api/weather/dublin 的 Postman 会返回:
{
"coord": {
"lon": -6.26,
"lat": 53.35
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 287.71,
"pressure": 1009,
"humidity": 82,
"temp_min": 287.15,
"temp_max": 288.15
},
"visibility": 10000,
"wind": {
"speed": 6.7,
"deg": 220
},
"clouds": {
"all": 75
},
"dt": 1539694800,
"sys": {
"type": 1,
"id": 5237,
"message": 0.0019,
"country": "IE",
"sunrise": 1539672865,
"sunset": 1539710737
},
"id": 2964574,
"name": "Dublin",
"cod": 200
}
【问题讨论】: