Libgdx Json Parsing From An Api
I'm trying to parse a JSON returned by OpenWeatherMap API, specifically this. I'm using the approach suggested in this post, that is create classes with class variables with names
Solution 1:
So, there were few solutions:
- Use an ObjectMap (link, at the very end)
- Parse it myself (the last resort)
or the one I'm currently employing successfully:
/*...*/Jsonjson=newJson(); StringjsonStr=/* JSON here */ jsonStr = jsonStr.replace("\"3h\"", "\"_3h\""); JSONWrapperjsonWrapper= json.fromJson(JSONWrapper.class, jsonStr); /*...*/
Accessing values:
double windSpeed = jsonWrapper.wind.speed;
And the wrapper class:
import java.util.ArrayList;
publicclassJSONWrapper{
Coord coord;
ArrayList<Weather> weather;
String base;
MainJ main;
Wind wind;
Clouds clouds;
Rain rain;
Snow snow;
int dt;
Sys sys;
int id;
String name;
int cod;
String message;
Visibility visibility;
}
classWeather{
int id;
String main;
String description;
String icon;
}
classCoord{
double lon;
double lat;
}
classVisibility{
String value;
}
classMainJ{
double temp;
int pressure;
int humidity;
double temp_min;
double temp_max;
}
classWind{
double speed;
int deg;
}
classClouds{
int all;
}
classSnow{
int _3h;
}
classRain{
int _3h;
}
classSys{
int type;
int id;
double message;
String country;
int sunrise;
int sunset;
}
Post a Comment for "Libgdx Json Parsing From An Api"