简体   繁体   English

JSonArray转换为String以使用GSon进行解析

[英]JSonArray to String for parsing with GSon

I have been give this method for parsing a JSonArray 我已经给了这种方法来解析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? 在那里调用toString()方法是否错误?

    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? 另外,这是将JSonArray转换为列表的次佳方法吗?

If the Json array received in the response is correct, 如果响应中收到的Json数组正确,

then there would be no error at toString to get the corresponding json. 那么在toString处获取相应的json就不会出错。

I would say please check the JSON object you are receiving in the json array is equivalent POJO for the User class. 我想说的是,请检查您在json数组中接收的JSON对象是否等同于User类的POJO。

hope, that helps!! 希望有帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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