这是一个单元素数组。我在Android中使用Retrofit2在Java中无法在通话中收到它
我正在使用这个模型

    @SerializedName("timelineitems")
    public List<HashMap<String,Data>> data;

    public class Data{

            @SerializedName("new_daily_cases")
            public String new_cases;

    }


但在最后一行找到对象字符串

            "stat": "ok"



这条线使呼叫运行失败

"timelineitems":[
{
    "4/17/20": {
                "new_daily_cases": 150,
                "new_daily_deaths": 16,
                "total_cases": 2418,
                "total_recoveries": 65,
                "total_deaths": 364
            },
            "4/18/20": {
                "new_daily_cases": 116,
                "new_daily_deaths": 3,
                "total_cases": 2534,
                "total_recoveries": 65,
                "total_deaths": 367
            },
            "stat": "ok"
        }
    ]

最佳答案

这就是我用来解决问题的模型
公共类模型{

@SerializedName("timelineitems")
public List<HashMap<String,Object>> data;

public class Data{

    @SerializedName("new_daily_cases")
    public String new_cases;
    @SerializedName("new_daily_deaths")
    public String new_daily_deaths;
    @SerializedName("total_cases")
    public String total_cases;
    @SerializedName("total_recoveries")
    public String total_recoveries;
    @SerializedName("total_deaths")
    public String total_deaths;

}


}

然后我用这段代码来获取我的数据

尝试{Model resource = response.body();

                   HashMap<String,Object>data=resource.data.get(0);

                    Gson g = new Gson();
                    Data inf = g.fromJson(g.toJson(resource.data.get(0).get(date0)), Data.class);



                }catch (NullPointerException e){
                   =
                    Toast.makeText(getApplicationContext(),"No data available",Toast.LENGTH_LONG).show();
                }

07-24 09:37
查看更多