第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 gson 和改造從對象讀取數(shù)據(jù)

無法使用 gson 和改造從對象讀取數(shù)據(jù)

江戶川亂折騰 2022-06-15 16:51:11
服務器當前返回一個類似于下面給出的 json 的 json,它包含一個名為的字段,data它是一個數(shù)組數(shù)組,每個數(shù)組有多個對象,每個對象都有一個名為item_type. 該鍵item_type允許我準確地確定它是什么類型的對象,以便我可以在 java 中相應地轉換它,但我無法讀取該item_type字段。{     "page":1,   "data":[        [           {              "id":1,            "name":"xyz",            "item_type":1         }      ],      [           {              "cid":1,            "item_type":2         }      ],      [           {              "item":1,            "item_type":3         }      ]   ]}改裝界面是這樣的    @GET("Search")        Observable<SearchResults> search(@Query(ParamsHolder.GET_QUERY)     String query, @Query(ParamsHolder.GET_PAGE) int page);SearchResults類看起來像這樣public class SearchResults {    int page;    List<List<Object>> data;    public int getPage() {        return page;    }    public List<List<Object>> getData() {        return data;    }}我能夠很好地獲取數(shù)據(jù),但我需要讀取每個列表中的對象。為此,我使用了以下代碼public class SearchItem {  int item_type;  public int getItemType() {      return item_type;  }}List<List<Object>> data = objects.getData();for (int i = 0; i < data.size(); i++) {  List<Object> list = data.get(i);  for (int j = 0; j < list.size(); j++) {    SearchItem item = (SearchItem) list.get(i);    Log.d("search", " json=" + list.get(j).toString());    Log.d("search", " item=" + item.toString() + " type=" + item.getItemType());    }}我創(chuàng)建了一個虛擬SearchItem類,我認為它會讓我對類型進行Object類型轉換,這樣我就可以閱讀item_type它會讓我確切地知道這是對象的類型。它拋出一個錯誤com.google.gson.internal.LinkedTreeMap cannot be cast to in.example.models.SearchItem這里的最終目標是能夠將數(shù)組中的每個對象轉換為正確的類型,而我能做到這一點的唯一方法是讀取item_type對象中的鍵
查看完整描述

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


查看完整回答
反對 回復 2022-06-15
?
慕容森

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();

        }


查看完整回答
反對 回復 2022-06-15
  • 2 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號