【问题标题】:Retrofit2 Data Model- Something wrong with the listRetrofit2 数据模型 - 列表有问题
【发布时间】:2017-04-18 12:54:57
【问题描述】:

我正在使用 OpenWeatherMap API 检索 16 天的预报信息。

http://www.json-generator.com/api/json/get/cuDhZDicMO?indent=2

从这样的 JSON 中,我能够检索到 cod 的值。这意味着 API 一切正常。我在调试时对其进行了测试,API 工作正常,因为我能够检索cod 的值。

但是我在ForecastInfo 数据模型中的列表有问题。你能看看那个并告诉我我哪里出错了吗?因为它没有检索该列表下的数据。我附上了我的forecastInfo,它是 JSON 的数据模型。非常感谢!

import java.util.ArrayList;
import java.util.List;

public class ForecastInfo {

public final double cod;

public List<LIST> list = new ArrayList<LIST>();

public List<LIST> getList() {
    return list;
}
public ForecastInfo(List<LIST> list,double cod)
{
    this.cod=cod;
    this.list=list;
}

class LIST {
    public final long dt;

    public final Temp temp;
   public LIST(long dt,Temp temp) {
        this.dt = dt;
        this.temp=temp;
    }

    class Temp
    {
        public final double day;

        public Temp(double day){
            this.day=day;

        }
    }
  }
}

【问题讨论】:

  • 您的映射应该可以工作。您面临的确切问题是什么?

标签: java json gson


【解决方案1】:

因为您的模型需要具有响应 json 文件包含的所有键,并且键的名称应该非常相同。为什么不使用http://www.jsonschema2pojo.org/ 站点将json 转换为pojo 检查源类型为JSON 和注释类型为gson。希望它可以帮助你。或者只是获取字符串类型的响应,然后将其转换为
LoginResponse loginResponse = new Gson().fromJson(json, LoginResponse.class);

【讨论】:

    猜你喜欢
    • 2020-05-18
    • 2018-07-26
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    相关资源
    最近更新 更多