How to grab JSON Array and use gson to parse each json object? (Retrofit)

后端 未结 4 1445
忘掉有多难
忘掉有多难 2020-12-30 23:05

I am returning an array of results with my json Objects, and I am trying to use my customObjectResponse class to pull out each of the fields within each of the objects... th

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 00:00

    You could try something like this

    JSONObject jsonObject = new JSONObject();
    JSONArray jsonArray = jsonObject.getJSONArray();
    
    //use GSON to parse
    if (jsonArray != null) {
       Gson gson = new Gson();
       ObjResponse[] objResponse = gson.fromJson(jsonArray.toString(), ObjResponse[].class);
       List objResponseList = Arrays.asList(objResponse);
    }
    

    This should definitely work.

提交回复
热议问题