简体   繁体   中英

JSonArray to String for parsing with GSon

I have been give this method for parsing a JSonArray

private List<User> parseUserList(String jsonArray) {
    Gson gson = new Gson();
    // Declares the list type
    Type listType = new TypeToken<List<User>>() {}.getType();
    List<User> userList = gson.fromJson(jsonArray, listType);
    return userList;
}

I'm getting and an error on the line where i call this method. Is it wrong to call the toString() method there?

    JsonArrayRequest jsonArray = new JsonArrayRequest(
            Request.Method.GET,
            USERS_URL,
            null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    List<User> list = parseUserList(response.toString());
                }
            },
            new Response.ErrorListener() {...}
            }
    )

Also, is this a suboptimal way to turn a JSonArray into a list?

If the Json array received in the response is correct,

then there would be no error at toString to get the corresponding json.

I would say please check the JSON object you are receiving in the json array is equivalent POJO for the User class.

hope, that helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM