0
내 Java 웹 응용 프로그램에 날씨 정보를 얻기 위해 wunderground를 사용하고 있습니다. 약간의 검색 후에 나는 좋은 json 파서와 같이 보이는 Gson을 발견했습니다.Gson은 BEGIN_OBJECT이 (가) 예상되었지만 STRING이 (가)
그러나 몇 가지 예에 따르면 루트 요소를 건너 뛰는 방법을 이해할 수없는 것 같습니다. 이제에만 관심이있어하는 부분은 forecastday입니다
{
"forecast" : {
"simpleforecast" : {
"forecastday" : [ {
"avehumidity" : 61,
"avewind" : {
"degrees" : 342,
"dir" : "NNW",
"kph" : 11,
"mph" : 7
},
"conditions" : "Clear",
"date" : {
"ampm" : "PM",
"day" : 17,
"epoch" : "1358478000",
"hour" : 21,
"isdst" : "0",
"min" : "00",
"month" : 1,
"monthname" : "January",
"pretty" : "9:00 PM CST on January 17, 2013",
"sec" : 0,
"tz_long" : "America/Chicago",
"tz_short" : "CST",
"weekday" : "Thursday",
"weekday_short" : "Thu",
"yday" : 16,
"year" : 2013
},
"high" : {
"celsius" : "11",
"fahrenheit" : "52"
},
"icon" : "clear",
"icon_url" : "http://icons-ak.wxug.com/i/c/k/clear.gif",
"low" : {
"celsius" : "-2",
"fahrenheit" : "28"
},
"maxhumidity" : 76,
"maxwind" : {
"degrees" : 340,
"dir" : "NNW",
"kph" : 18,
"mph" : 11
},
"minhumidity" : 38,
"period" : 1,
"pop" : 10,
"qpf_allday" : {
"in" : 0.0,
"mm" : 0.0
},
"qpf_day" : {
"in" : 0.0,
"mm" : 0.0
},
"qpf_night" : {
"in" : 0.0,
"mm" : 0.0
},
"skyicon" : "mostlysunny",
"snow_allday" : {
"cm" : 0,
"in" : 0
},
"snow_day" : {
"cm" : 0,
"in" : 0
},
"snow_night" : {
"cm" : 0,
"in" : 0
}
},
{
"avehumidity" : 59,
"avewind" : {
"degrees" : 172,
"dir" : "South",
"kph" : 6,
"mph" : 4
},
"conditions" : "Clear",
"date" : {
"ampm" : "PM",
"day" : 18,
"epoch" : "1358564400",
"hour" : 21,
"isdst" : "0",
"min" : "00",
"month" : 1,
"monthname" : "January",
"pretty" : "9:00 PM CST on January 18, 2013",
"sec" : 0,
"tz_long" : "America/Chicago",
"tz_short" : "CST",
"weekday" : "Friday",
"weekday_short" : "Fri",
"yday" : 17,
"year" : 2013
},
"high" : {
"celsius" : "10",
"fahrenheit" : "50"
},
"icon" : "clear",
"icon_url" : "http://icons-ak.wxug.com/i/c/k/clear.gif",
"low" : {
"celsius" : "-1",
"fahrenheit" : "30"
},
"maxhumidity" : 84,
"maxwind" : {
"degrees" : 170,
"dir" : "South",
"kph" : 10,
"mph" : 6
},
"minhumidity" : 40,
"period" : 2,
"pop" : 0,
"qpf_allday" : {
"in" : 0.0,
"mm" : 0.0
}, ..................
: 는 여기 예측 JSON 요청의 예입니다.
package entity.json;
import java.util.List;
public class Data {
private List<Forecast> forecastday;
public List<Forecast> getForecastdays() {
return forecastday;
}
}
예측 클래스 : 여기
package entity.json;
public class Forecast {
private int avehumidity;
private Avewind avewind;
private String conditions;
private DateWeather date;
private CelciusHigh high;
private String icon_url;
private CelciusLow low;
// + constructor, getters, setters
내 방법 : 그래서 내가 한 것은 다음과 같은 지금
public void createWeatherForecast(){
String text = "";
try {
URL url = new URL(jsonUrl);
System.out.println(jsonUrl);
Scanner s = new Scanner(url.openStream());
while(s.hasNextLine()){
text += s.nextLine();
}
Data weather = new Gson().fromJson(text, Data.class);
System.out.println(new Gson().toJson(weather));
System.out.println(weather.getForecastdays().get(0).getAvewind());
} catch (IOException ex) {
Logger.getLogger(WeatherController.class.getName()).log(Level.SEVERE, null, ex);
}
}
나는 또한 예측에 국한해야한다가와 간단한 일기 예보?
늦게 답변을 드려 죄송합니다.이 질문에 대해서는 완전히 잊었습니다. 당신이 제안한대로 나는 내 자신의 비 직렬화기를 썼다. 동료 프로그래머의 도움을 받아 : D – VercauterenP