2

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?

durron597
  • 31,968
  • 17
  • 99
  • 158
Jonas
  • 769
  • 1
  • 8
  • 37
  • I think it's ok, http://api.openweathermap.org/data/2.5/weather?q=gent – Jonas Aug 19 '15 at 08:48
  • I think you have to use the `json keys` as variables in your `POJO` class. i.e, variable names and `json keys` has to same. Also may be its better to write a `JsonParser` class of your own instead of using `Gson`. – Emil Aug 19 '15 at 09:00
  • 1
    No man, gson is very excellent and much less work than a manual jsonparser – Jonas Aug 19 '15 at 09:54
  • Why did you remove all the quotes from the JSON in that link? – durron597 Aug 19 '15 at 13:35
  • @Guil You are not showing us the code that is the problem. It looks just fine. Are you sure that your retrofit parsing is working correctly? Can you verify that the response is the same JSON as you get when you use your web browser? – durron597 Aug 19 '15 at 13:40
  • @Guil: The error means that Gson is expecting an opening curly brace but instead it finds a string. But since the JSON you are showing *does* begin with a curly brace I also do not think that that is the problem. Maybe there is something wrong the request and you are getting an (HTML) error message instead? Try turning on Retrofit's logging and see what the actual response body looks like... – david.mihola Aug 20 '15 at 07:15
  • @immibis@ Is it still not valid JSON, or has someone fixed it? – Caelum Aug 26 '15 at 13:21
  • @user2104648 It was fixed. – user253751 Aug 27 '15 at 00:12
  • @immibis pls share how u fixed ... i put my 24 hours but still same error – Erum Mar 02 '17 at 07:04
  • @Erum I meant someone fixed the JSON in the question. Wtf are you trying to ask me? – user253751 Mar 02 '17 at 20:26

0 Answers0