从JsonElement获取值。转换为多个数组

时间:2018-01-27 03:11:17

标签: android arrays json retrofit

所以我想将JsonElement转换为多个或一个大数组。我不确定什么是正确的想法。可悲的是,我甚至不知道如何开始。也许有人可以帮助我。如果需要自己转换JsonElement,为什么我甚至需要Pojo类。如果有人能指出我的问题,我会很高兴。

Api回应:

{
    "1": {
        "symbol": "BTC",
        "cap": "188545805889",
        "change": {
            "hour": "1.57",
            "day": "-2.97"
        },
        "price": "11203.9",
        "coinheat": 73,
        "url": "https://chasing-coins.com/coin/BTC"
    },
    "2": {
        "symbol": "ETH",
        "cap": "102905943936",
        "change": {
            "hour": "1.3",
            "day": "-1.23"
        },
        "price": "1058.34",
        "coinheat": 73,
        "url": "https://chasing-coins.com/coin/ETH"
    },
    "3": {
        "symbol": "XRP",
        "cap": "46820128001.0",
        "change": {
            "hour": "0.82",
            "day": "-8.28"
        },
        "price": "1.2086",
        "coinheat": 82,
        "url": "https://chasing-coins.com/coin/XRP"
    }
}

Pojo课程:

public class Coinstats {

    @SerializedName("symbol")
    @Expose
    private String symbol;
    @SerializedName("cap")
    @Expose
    private String cap;
    @SerializedName("change")
    @Expose
    private Change change;
    @SerializedName("price")
    @Expose
    private String price;
    @SerializedName("coinheat")
    @Expose
    private Integer coinheat;
    @SerializedName("url")
    @Expose
    private String url;

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getCap() {
        return cap;
    }

    public void setCap(String cap) {
        this.cap = cap;
    }

    public Change getChange() {
        return change;
    }

    public void setChange(Change change) {
        this.change = change;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public Integer getCoinheat() {
        return coinheat;
    }

    public void setCoinheat(Integer coinheat) {
        this.coinheat = coinheat;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}

致电:  ç

all2.enqueue(new Callback<JsonElement>() {
            @Override
            public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

                JsonObject object = response.body().getAsJsonObject();


            }
            @Override
            public void onFailure(Call<JsonElement> call, Throwable t) {
                Log.w("no Stats", t.toString());
            }
        });

3 个答案:

答案 0 :(得分:0)

您可以使用Android网络库

https://github.com/amitshekhariitbhu/Fast-Android-Networking

响应时你可以创建JsonObject

JSONObject currency = new JSONObject(response.toString()); JSONArray lisOfcur = currency.getJSONArray("list_of_cur");

答案 1 :(得分:0)

为什么在手动解析JSON时使用Retrofit而不是你可以使用Volley,如果你想在JSON对象中使用JSON对象做这样的事情,如果你想在列表中同时进行小时和日,那么你POJO class必须包含另一个POJO类型的数组列表的getter和setter,可能就是Time。

@SerializedName("change")
@Expose
private ArrayList<Time> time;

你必须制作getter和setter,你的列表项将包含一个时间列表。但是在每个JSON对象中都包含一个“更改”对象,因此您不需要创建数组列表,您只需为时间设置getter setter。

@SerializedName("change")
@Expose
private Time time;

答案 2 :(得分:0)

有了这个,您应该能够解析API响应。请注意,当API响应更改时,此代码可能无法正常工作。

描述:将json String打开到JSONObject,然后使用keys遍历Iterator并根据需要创建新的JSONObject。 我将硬币数据加载到新的CoinData对象中并将其打包成ArrayList

从API源下载json字符串后调用此方法。

private ArrayList<CoinData> getCoinData(String jsonString){
    ArrayList<CoinData> list = null;

    try {
        list = new ArrayList<>();

        JSONObject json = new JSONObject(jsonString);
        Iterator<String> it = json.keys();
        while (it.hasNext()){
            String p = it.next();
            JSONObject o = json.getJSONObject(p);
            String symbol = o.optString("symbol", "symbol");
            String cap = o.optString("cap", "cap");
            String price = o.optString("price", "price");
            String coinheat = o.optString("coinheat", "coinheat");
            String url = o.optString("url", "url");
            JSONObject change = o.getJSONObject("change");
            String hour = change.optString("hour", "hour");
            String day = change.optString("day", "day");

            Log.e(TAG, "keys = " + p);
            Log.e(TAG, "symbol = " + symbol + " cap = " + cap + " hour = " + hour + "  day = " + day);

            CoinData data = new CoinData();
            data.setCap(cap);
            data.setCoinheat(coinheat);
            data.setDay(day);
            data.setHour(hour);
            data.setPrice(price);
            data.setSymbol(symbol);
            data.setUrl(url);
            list.add(data);
        }
    }
    catch (Exception ex){
        Log.e(TAG, "getCoinData  ----  " + ex.getMessage());
    }
    return list;
}

现在创建一个新类 - 让我们用它的公共getter和setter调用它CoinData,你可以访问数据并将硬币数据存储在列表对象中。

public class CoinData {

    private static final String TAG = CoinData.class.getSimpleName();

    String symbol = "";
    String cap = "";
    String price = "";

    String coinheat = "";
    String url = "";
    String hour = "";
    String day = "";


    public void setCap(String cap) { this.cap = cap; }
    public void setSymbol(String symbol) { this.symbol = symbol; }
    public void setPrice(String price) { this.price = price; }
    public void setCoinheat(String coinheat) { this.coinheat = coinheat; }
    public void setUrl(String url) { this.url = url; }
    public void setDay(String day) { this.day = day; }
    public void setHour(String hour) { this.hour = hour; }

    public String getCap() { return this.cap; }
    public String getSymbol() { return this.symbol; }
    public String getPrice() { return this.price; }
    public String getCoinheat() { return this.coinheat; }
    public String getUrl() { return this.url; }
    public String getHour() { return this.hour; }
    public String getDay() { return this.day; }

}

!!稍后当您将数据传递到RecyclerView时,您只需通过ArrayList<CoinData>,并在onBindViewHolder方法中使用CoinData的公共getter !! < / p>

ArrayList <CoinData> mCoinData;
public MyAdapter(ArrayList <CoinData> coinData) {
    this.mCoinData = coinData;
}

而不是:

 public MyAdapter(ArrayList <String> coin,ArrayList <String> cap,ArrayList <String> price,ArrayList <String> day) {

这将使您的代码更灵活,更易于处理。