使用GsonConverterFactory

时间:2018-01-19 13:31:50

标签: java android json retrofit2

我有这个gson格式:

{
"ltc_btc":{
    "high":105.41,
    "low":104.67,
    "avg":105.04,
    "vol":43398.22251455,
    "vol_cur":4546.26962359,
    "last":105.11,
    "buy":104.2,
    "sell":105.11,
    "updated":1418654531
}
...

我试图通过GsonCoverterFactory解析它,但它只有在声明它时才有效:

@SerializedName("ltc_btc")
@Expose
TickerPoint pair;

但它没有解决问题的原因" ltc_bts"永远是不同的。有什么想法吗?

P.S我尝试了HashMap - 相同的NPE。

3 个答案:

答案 0 :(得分:0)

确切地说,'ltc_bts'键是你的Object类名。您需要将json反序列化为对象。

只需在参数中传递Class,然后在需要时将其与参数类进行序列化。

答案 1 :(得分:0)

为“ltc_btc”创建一个类(或者它是什么,也许是“eth_btc。如果值Object是相同的,你需要一个模板)。

public class CryptoCurrencyExchangeModel {
//one of this fields for you must be primary. that field must never be null

    @SerializedName("high")
    private Double high;

    @SerializedName("low")
    private Double low;

    @SerializedName("avg")
    private Double avg;

    @SerializedName("vol")
    private Double vol;

    @SerializedName("vol_cur")
    private Double vol_cur;

    @SerializedName("last")
    private Double last;

    @SerializedName("buy")
    private Double buy;

    @SerializedName("sell")
    private Double sell;

    @SerializedName("updated")
    private Long updated;


}

现在需要将json解析为JsonObject。在这种情况下,json是一个String,您将从Retrofit响应中获得。

JSONObject currencyJsonObject = new JSONObject("{
"ltc_btc":{
    "high":105.41,
    "low":104.67,
    "avg":105.04,
    "vol":43398.22251455,
    "vol_cur":4546.26962359,
    "last":105.11,
    "buy":104.2,
    "sell":105.11,
    "updated":1418654531
}");

之后,你需要从JsonObject获取密钥并迭代它并获得你想要的对象。

    Iterator<?> keys = jObject.keys();
    CryptoCurrencyExchangeModel cryptoCurrencyExchangeModel;
    Gson gson;
    while( keys.hasNext() ) {
        String key = (String)keys.next();
        if ( jObject.get(key) instanceof JSONObject ) {
           cryptoCurrencyExchangeModel = gson.fromJson(jObject.get(key).toString(), CryptoCurrencyExchangeModel);
           if(cryptoCurrencyExchangeModel != null && cryptoCurrencyExchangeModel.buy != null){
      //here you do what you want
}
        }
    }

答案 2 :(得分:0)

有解决方案:

public Observable<TickerResponse> getTicker(YobitPublic api, String first_coin, String second_coin) {
    Call<JsonObject> call = api.getTickerObservable(first_coin + "_" + second_coin);
    return Observable.create( emitter -> {
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                Log.d(TAG, "Response code: " + response.code());
                JsonObject jsonObject = response.body();
                Gson gson = new Gson();
                TickerResponse tickerResponse;
                Set<Map.Entry<String,JsonElement>> set = jsonObject.entrySet();
                for (Map.Entry<String,JsonElement> obj : set) {
                    String key = obj.getKey();
                    if (jsonObject.get(key) != null) {
                        TickerPoint point = gson.fromJson(jsonObject.get(key).toString(), TickerPoint.class);
                        if (point != null && point.getServer_time() != 0) {
                            tickerResponse = new TickerResponse(key, point);
                            emitter.onNext(tickerResponse);
                            emitter.onComplete();
                            return;
                        }
                    }
                }
                emitter.onError(new Throwable("Error"));
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                emitter.onError(t);
            }
        });
    });

}