我想要使用retrofit2和GSON來使用JSON。如何在Retrofit2中準備JSON結果(在發送到GsonConverterFactory之前)
以下是服務器提供的響應。請注意,「d」的值是有效JSON的字符串(一旦刪除斜槓)。
{ 「d」:「 [{\」 號碼\ 「:\」 2121 \ 「\ 」NumberOfAppearances \「:2,\ 」獎品\「:
[{\」 DrawDate \ 「:\」\/Date(1439654400000)\/\「,\」PrizeCode \「:\」S \「}, {\」DrawDate \「:\」\/Date(874771200000)\/\「 「PrizeCode \」:\ 「S \」}]}] 「}
是否有使用retrofit2在通話過程中preparse的json的,以retrofitService得到d的值內的對象的方式?
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
//is there anything i can do here to do preparsing of the results?
.addConverterFactory(GsonConverterFactory.create())
.build();
IQueryLocations = retrofit.create(IMyQuery.class);
//currently GsonResults is the string value of d, instead of the JSON objects
Call<GsonResult> result = IMyQuery.doQuery("2121");
理想情況下,我想插入addConverterFactory前一個方法調用做preparsing
的preparsing方法的輸出會像下面的一些事情:
{「d」 :[{「Number」:「2121」,「NumberOfAppearances」:2,「獎品」:
[{「DrawDate」:1439654400000,「PrizeCode」:「S」}, {「DrawDate」:874771200000,「PrizeCode 「:」S「}]}]}
這是不可能preparse的響應,你沒有在開始的反應。因此,最簡單的方法是從改造中獲得原始響應,然後根據您的需要進行解析。 –