오픈웨더웹 GSON 파싱질문
조회수 1460회
오픈웨더웹에서 Retrofit2 를 사용해서 데이터를 가져오는걸 테스트 하고 있습니다 그런데 데이터는 잘받아오는데 GSON 파싱부분중 모르는 부분이 있어서 질문하려고 합니다
{
"coord": {
"lon": 127,
"lat": 37
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}
],
"base": "stations",
"main": {
"temp": 274.88,
"pressure": 1017,
"humidity": 26,
"temp_min": 274.15,
"temp_max": 276.15
},
"visibility": 11265,
"wind": {
"speed": 4.1,
"deg": 320,
"gust": 7.2
},
"clouds": {
"all": 20
},
"dt": 1488862800,
"sys": {
"type": 1,
"id": 7673,
"message": 0.0139,
"country": "KR",
"sunrise": 1488837281,
"sunset": 1488879111
},
"id": 1838431,
"name": "Pyeong",
"cod": 200
}
이게 파싱할 데이터들이고
public class WatherData{
@SerializedName("coord")
Coord coprd;
@SerializedName("weather")
Weather wather;
@SerializedName("main")
Main main;
public class Coord {
@SerializedName("lon")String lon;
@SerializedName("lat")String lat;
public String getLon() {
return lon;
}
public String getLat() {
return lat;
}
@Override
public String toString() {
return "Coord{" +
"lon='" + lon + '\'' +
", lat='" + lat + '\'' +
'}';
}
}
public class Weather{
@SerializedName("id") String id;
@SerializedName("main") String main;
@SerializedName("description") String description;
@SerializedName("icon") String icon;
@Override
public String toString() {
return "Wather{" +
"id='" + id + '\'' +
", main='" + main + '\'' +
", description='" + description + '\'' +
", icon='" + icon + '\'' +
'}';
}
}
public class Main{
@SerializedName("temp") String temp;
@SerializedName("pressure") String pressure;
@SerializedName("humidity") String humidity;
@SerializedName("temp_min") String temp_min;
@SerializedName("temp_max") String temp_max;
@Override
public String toString() {
return "Main{" +
"temp='" + temp + '\'' +
", pressure='" + pressure + '\'' +
", humidity='" + humidity + '\'' +
", temp_min='" + temp_min + '\'' +
", temp_max='" + temp_max + '\'' +
'}';
}
}
@Override
public String toString() {
return "WatherData{" +
"coprd=" + coprd.toString() +
// ", wather=" + wather.toString() +
", main=" + main.toString() +
'}';
}
}
```
파서클레스입니다
파싱할 데이터중에서 coord , weather, main 만 파싱하려고 합니다
그런데 이중에 weather이 list로 데이터를 얻어오는데 이걸 어찌 GSON으로 파싱하는지 모르겠습니다
list데이터에 따로 이름이 붙어 있으면 파싱할수 있는데 이름이 아예 없는건 어찌 파싱하는건지 모르겠습니다
-
(•́ ✖ •̀)
알 수 없는 사용자
2 답변
-
으히히힠!
해결했습니다
http://stackoverflow.com/questions/39361159/parsing-json-object-to-json-array-using-gson
요렇게 하는거였군요....
스택오브플로우는 갓갓갓!!!!
-
(•́ ✖ •̀)
알 수 없는 사용자
-
-
스택오버플로우에 채택된 답변이 없어서 무엇을 참고할지 모르는 분이 계실 수도 있어,
제가 GSON 파싱 관련해서 답변했던 글을 추가해봅니다.
댓글 입력