猿问

无法使用 gson 和改造从对象读取数据

服务器当前返回一个类似于下面给出的 json 的 json,它包含一个名为的字段,data它是一个数组数组,每个数组有多个对象,每个对象都有一个名为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;

    }

}

我能够很好地获取数据,但我需要读取每个列表中的对象。为此,我使用了以下代码



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

    }


}


我创建了一个虚拟SearchItem类,我认为它会让我对类型进行Object类型转换,这样我就可以阅读item_type它会让我确切地知道这是对象的类型。


它抛出一个错误com.google.gson.internal.LinkedTreeMap cannot be cast to in.example.models.SearchItem


这里的最终目标是能够将数组中的每个对象转换为正确的类型,而我能做到这一点的唯一方法是读取item_type对象中的键


江户川乱折腾
浏览 109回答 2
2回答

小怪兽爱吃肉

使用 Gson 直接解析响应更新搜索结果&nbsp;public class SearchResults {&nbsp; &nbsp; int page;&nbsp; &nbsp; List<List<SearchItem>> data;&nbsp; &nbsp; public int getPage() {&nbsp; &nbsp; &nbsp; &nbsp; return page;&nbsp; &nbsp; }&nbsp; &nbsp; public List<List<SearchItem>> getData() {&nbsp; &nbsp; &nbsp; &nbsp; return data;&nbsp; &nbsp; }}您可以像这样访问 item_typesearchResult.getData.get(i).get(0).item_type

慕容森

在您的接口改造函数中使用 Response 作为返回类型,如下所示@GET("Search")Observable<Response<ResponseBody>> search(@Query(ParamsHolder.GET_QUERY)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String query, @Query(ParamsHolder.GET_PAGE) int page);这样您就可以检索完整的 json 正文,并且您现在可以完全控制如何根据 item_type 解析数据所以基本上你调用函数 response.body().string() 来获取json体而不是您手动解析,如下所示:&nbsp;JSONObject jsonObject = null;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObject = new JSONObject(response.body().string());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = jsonObject.getJSONArray("data");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //looping through the data array&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0;i<jsonArray.length();i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray&nbsp; childArr = jsonArray.getJSONArray(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//looping through current child array inside data&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0;j<childArr .length();j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject nestedChildObj =childArr.getJSONObject(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//now i have access to item_type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int item_type =&nbsp; nestedChildObj .getInt("item_type")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//now you can use gson to parse the nestedChildObj based&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//on item_type or manually do the parsing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答