2 回答

TA貢獻1852條經(jīng)驗 獲得超1個贊
使用 Gson 直接解析響應
更新搜索結果
public class SearchResults {
int page;
List<List<SearchItem>> data;
public int getPage() {
return page;
}
public List<List<SearchItem>> getData() {
return data;
}
}
您可以像這樣訪問 item_type
searchResult.getData.get(i).get(0).item_type

TA貢獻1853條經(jīng)驗 獲得超18個贊
在您的接口改造函數(shù)中使用 Response 作為返回類型,如下所示
@GET("Search")
Observable<Response<ResponseBody>> search(@Query(ParamsHolder.GET_QUERY)
String query, @Query(ParamsHolder.GET_PAGE) int page);
這樣您就可以檢索完整的 json 正文,并且您現(xiàn)在可以完全控制如何根據(jù) item_type 解析數(shù)據(jù)
所以基本上你調用函數(shù) response.body().string() 來獲取json體
而不是您手動解析,如下所示:
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response.body().string());
JSONArray jsonArray = jsonObject.getJSONArray("data");
//looping through the data array
for(int i = 0;i<jsonArray.length();i++){
JSONArray childArr = jsonArray.getJSONArray(i);
//looping through current child array inside data
for(int j = 0;j<childArr .length();j++){
JSONObject nestedChildObj =childArr.getJSONObject(j);
//now i have access to item_type
int item_type = nestedChildObj .getInt("item_type")
//now you can use gson to parse the nestedChildObj based
//on item_type or manually do the parsing
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
添加回答
舉報