I am trying to parse JSON which looks like:
{
"coord": {
"lon": 3.72,
"lat": 51.05
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "Sky is Clear",
"icon": "01d"
}
],
"base": "cmc stations",
"main": {
"temp": 295.33,
"pressure": 1020,
"humidity": 43,
"temp_min": 294.15,
"temp_max": 296.48
},
"wind": {
"speed": 3.6,
"deg": 220
},
"clouds": {
"all": 0
},
"dt": 1439990536,
"sys": {
"type": 3,
"id": 4839,
"message": 0.004,
"country": "BE",
"sunrise": 1439959100,
"sunset": 1440010677
},
"id": 2797656,
"name": "Gent",
"cod": 200
}
To make Java objects, I used a retrofit response with a callback, which worked. After the retrofit part I called it up and used Gson to parse the JSON response to a WeatherData
class. To test if it worked properly, I log the cityname.
restAdapter.getApi().getWeatherFromApi(txtCity.getText().toString(), new Callback<WeatherData>() {
@Override
public void success(WeatherData weatherData, Response response) {
Gson gson = new Gson();
WeatherData data = gson.fromJson(response.toString(), WeatherData.class);
Log.i(TAG, data.getName());
}
But here it's going wrong, when I run the line where I initialize data, I get the following error:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
Is something wrong with my POJO classes? I used following WeatherData class:
public class WeatherData {
@SerializedName("name")
private String name;
@SerializedName("main")
private Main main;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
}
For getting the minimum and maximum temperature, I created another POJO, Main.class
public class Main {
@SerializedName("temp_min")
private double minTemp;
@SerializedName("temp_max")
private double maxTemp;
public double getMinTemp() {
return minTemp;
}
public void setMinTemp(double minTemp) {
this.minTemp = minTemp;
}
public double getMaxTemp() {
return maxTemp;
}
public void setMaxTemp(double maxTemp) {
this.maxTemp = maxTemp;
}
}
I guess the BEGIN_OBJECT is the first {
of a JSON String, but I can't fix it. What is wrong?