JSON对象内而不是数组中的多个JSON对象

时间:2017-09-25 13:25:26

标签: android arrays json object

我正在使用提供以下响应的API。 (为简单起见,我已将Data中的对象数量减少到2个。)

{
"Response": "Success",
"Message": "Coin list succesfully returned!",
"BaseImageUrl": "https://www.cryptocompare.com",
"BaseLinkUrl": "https://www.cryptocompare.com",
"Data": {
          "42": {
          "Id": "4321",
          "Url": "/coins/42/overview",
          "ImageUrl": "/media/19984/42.png",
          "Name": "42",
          "CoinName": "42 Coin",
          "FullName": "42 Coin (42)",
          "Algorithm": "Scrypt",
          "ProofType": "PoW",
          "FullyPremined": "0",
          "TotalCoinSupply": "42",
          "PreMinedValue": "N/A",
          "TotalCoinsFreeFloat": "N/A",
          "SortOrder": "34"
          },
          "365": {
          "Id": "33639",
          "Url": "/coins/365/overview",
          "ImageUrl": "/media/352070/365.png",
          "Name": "365",
          "CoinName": "365Coin",
          "FullName": "365Coin (365)",
          "Algorithm": "X11",
          "ProofType": "PoW/PoS",
          "FullyPremined": "0",
          "TotalCoinSupply": "2300000000",
          "PreMinedValue": "N/A",
          "TotalCoinsFreeFloat": "N/A",
          "SortOrder": "916"
          }
},
"Type": 100
}

我想将Data中的所有对象放在一个数组中,以便我可以将它们传递给我的RecyclerView适配器。

我使用Retrofit2和RxJava2进行网络操作。

这是我目前改装生成器的代码:

 Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();

我还使用:http://www.jsonschema2pojo.org/

生成了POJO类Coin和Data

我已经编辑了类Data,因此它包含一个Coin数组而不是一个对象:

public class Data {
@SerializedName("Coin")
    @Expose
    private Coin[] coins;

    public Coin[] getCoin() {
        return coins;
    }

    public void setCoins(Coin[] coins){}
}

如何将Data中的所有对象添加到Coin数组? 我是否需要创建自定义转换器,如果是,我该如何实现?

提前致谢!

2 个答案:

答案 0 :(得分:0)

public class All_Plan  {

    @SerializedName("Coin")
    private ArrayList<CoinData> data;

    @SerializedName("status")
    private String status;

    public ArrayList<CoinData> getData() {
        return data;
    }

    public void setData(ArrayList<CoinData> data) {
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

答案 1 :(得分:0)

这是您的数据类:

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;


@JsonAdapter(Data.DataJsonAdapter.class)
public class Data {
    private Coin[] coins;

    public Coin[] getCoin() {
        return coins;
    }

    public void setCoins(Coin[] coins) {
         this.coins = coins;
    }

    public static class DataJsonAdapter extends TypeAdapter<Data> {
        @Override
        public void write(JsonWriter out, Data value) throws IOException {
            // TODO: Maybe implement
        }

        @Override
        public Data read(JsonReader reader) throws IOException {
            ArrayList<Coin> coins = new ArrayList<>();
            Gson gson = new Gson();
            reader.beginObject();
            while (reader.hasNext()) {
                reader.nextName(); // Read "42", "365", ...
                coins.add(gson.fromJson(reader, Coin.class));
            }
            reader.endObject();

            Data data = new Data();
            // Can probably be simplified
            Coin[] array = new Coin[coins.size()];
            coins.toArray(array); // fill the array
            data.setCoins(array);

            return data;
        }
    }
}

然后在你的代码中,只需调用:

Gson gson = new Gson();
Data data = gson.fromJson(yourJson, Data.class);

我还没有,但你应该在读取阅读器时检查是否有下一个预期的类型,以防止崩溃。