为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

江户川乱折腾 2022-06-15 16:51:11
服务器当前返回一个类似于下面给出的 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对象中的键
查看完整描述

2 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超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条经验 获得超18个赞

在您的接口改造函数中使用 Response 作为返回类型,如下所示


@GET("Search")

Observable<Response<ResponseBody>> search(@Query(ParamsHolder.GET_QUERY) 

              String query, @Query(ParamsHolder.GET_PAGE) int page);

这样您就可以检索完整的 json 正文,并且您现在可以完全控制如何根据 item_type 解析数据


所以基本上你调用函数 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 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号