预计BEGIN_OBJECT但在STRING第1列第1列路径

时间:2015-08-19 08:44:58

标签: java android json gson retrofit

我正在尝试解析看起来像的JSON:

{
  "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
}

为了制作Java对象,我使用了带回调的改进响应,这有效。在改造后的部分我调用它并使用Gson将JSON响应解析为WeatherData类。要测试它是否正常工作,我记录城市名称。

 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());
                    }

但是这里出错了,当我运行初始化数据的行时,我收到以下错误:

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:   预期BEGIN_OBJECT但是在第1行第1列路径$ STRING               在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:200)

我的POJO课程有问题吗?我使用了以下WeatherData类:

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;
    }
}

为了获得最低和最高温度,我创建了另一个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;
    }
}

我猜BEGIN_OBJECT是JSON字符串的第一个{,但我无法修复它。有什么问题?

0 个答案:

没有答案