无法在Android中使用Retrofit和Java创建POJO [英] Unable to create POJO with Retrofit and Java in android

查看:72
本文介绍了无法在Android中使用Retrofit和Java创建POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从供应商之一获得json响应.

I am getting following json response from one of the vendor.

{
  "status": "success",
  "data": {
  "values": [
      [
        "2015-12-28T09:15:00+0530",
        1386.4,
        1388,
        1381.05,
        1385.1,
        788
      ],
      [
        "2015-12-28T09:15:00+0530",
        1386.4,
        1388,
        1381.05,
        1385.1,
        788
      ]
    ]
  }
}

我想将其转换为POJO.

I would like to convert this to POJO.

我正在使用翻新版2.0,rx java.

I am using retrofit 2.0, rx java.

我尝试关注

public class HistoricalDataContainer implements Parcelable{
public String status;
public CandleList data;

protected HistoricalDataContainer(Parcel in) {
    status = in.readString();
    data = in.readParcelable(CandleList.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(status);
    dest.writeParcelable(data, flags);
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<HistoricalDataContainer> CREATOR = new Creator<HistoricalDataContainer>() {
    @Override
    public HistoricalDataContainer createFromParcel(Parcel in) {
        return new HistoricalDataContainer(in);
    }

    @Override
    public HistoricalDataContainer[] newArray(int size) {
        return new HistoricalDataContainer[size];
    }
};
}

还有

public class CandleList implements Parcelable{
public ArrayList<ArrayList<String>> values = new ArrayList<>();// doesn't work
public ArrayList<String[]> values=new ArrayList<String[]>();// doesn't work
public ArrayList<String>[] values; // doesn't work

protected CandleList(Parcel in) {
}

@Override
public void writeToParcel(Parcel dest, int flags) {
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<CandleList> CREATOR = new Creator<CandleList>() {
    @Override
    public CandleList createFromParcel(Parcel in) {
        return new CandleList(in);
    }

    @Override
    public CandleList[] newArray(int size) {
        return new CandleList[size];
    }
};
}

但是在上面的代码中,值"始终为null. 我想念的是什么.

But in the above code "value" is always null. What am I missing.

推荐答案

首先,您需要标识JSON结构中的对象,在这种情况下,有两个对象.您正在接收的json和2. data. 选项是为每个对象创建一个类.

First you need to identify the objects that are in the JSON structure, in this case there are two.1. The json that you are receiving and 2. data . The option is to create a class for every object.

第一类包含一个主要对象(json本身),其主要属性为:statusdata.

the first class contains the main object (json itself) with his main attributes: status and data.

public class HistoricalDataContainer implements Parcelable {
    private string status;
    private Data data;
    setters - getters
    ...
}

数据是一个对象,因此您需要创建自己的类来处理其属性,在这种情况下,该类是带有字符串数组的数组

data is an object, so you need to create his own class to handle his attributes, in this case is an array with string arrays

    public class Data implements Parcelable {
            private List<String> values;
            setters - getters
            ...
        }

要在值中获取特定的数组,您将需要执行以下操作:

To get an specific array inside values you are going to do something like:

List<String> myStringArray = historicalDataContainer.getData().values().get(index);

AND

您为什么使用Parcelable?

Why are you using Parcelable?

... 希望这个答案是您所需要的!

... I hope this answer is what you need!

这篇关于无法在Android中使用Retrofit和Java创建POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆