【问题标题】:Get an arraylist from JSON response从 JSON 响应中获取数组列表
【发布时间】:2019-11-25 00:23:54
【问题描述】:

我有一个节点 js API,我正在使用 Volley 从它获取 JSON 响应到 android,API 返回选择查询的结果,我需要在 android 中的对象 'News' 的 Arraylist 中获取此结果部分 。 这是节点 js API 部分:

    connection.query("SELECT * FROM news",
        function(err,rows,fields){
       if (!err){
        if (rows.length > 0){
            res.setHeader('Content-Type', 'application/json');
            res.send(rows);
        }
        else{
            var error = {msg: "news is empty"}
            res.send(error);
        }
    }

});

这是我需要从数组列表中的 json 响应中获取“行”的 android 部分:

  String URLnews = "http://10.0.2.2:3000/getnews";
        RequestQueue requestQueueNews = Volley.newRequestQueue(this);
        JsonObjectRequest objectRequestNews = new JsonObjectRequest(
                Request.Method.GET,
                URLnews,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject responsenews) {
                        Log.e("news length",responsenews.toString());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Rest Response",error.toString());
                    }
                }
        );


        requestQueueNews.add(objectRequestNews);

这是 JSON 响应:

[
    {"idnews":1,"content":"news first news","image":"img"}, 
    {"idnews":2,"content":"news 2","image":"img2"}
]

请注意,API 工作正常,它返回结果,但作为在 android 中打印的字符串,但我需要在 arraylist 中的单独对象中获取每一行,这可能吗?

【问题讨论】:

  • JSON 响应是什么样的?请包含示例响应。
  • 已经收录了,谢谢

标签: java android json android-volley


【解决方案1】:

因为您的 JSON 对象在 JSON 数组中,所以您需要将您的 JsonObjectRequest 更改为 JsonArrayRequest。

new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray responsenews) {
                    try {
                          for (int i = 0; i < responsenews.length(); i++) {
                              JSONObject object = responsenews.getJSONObject(i);
                              int id = object.getInt("idnews");
                              String content = object.getString("content");
                              String image = object.getString("image");
                        }
                    }catch (JSONException e) {
                         e.printStackTrace();
                    }
                }
            }

【讨论】:

    【解决方案2】:

    这里你缺少的是从 JSON 的转换。例如,您可以使用 GSON 来完成工作。

    new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray responsenews) {
              Log.e("news length",responsenews.toString());
              Gson gson = new Gson();
              Type type = new TypeToken<List<News>>() {}.getType();
              List<News> news = gson.fromJson(responsestr, listType);
           }
        },
    

    您需要定义 News 类,仅此而已。

    注意:答案已更新以反映 valentino 正确注意到的事实,即应该是 JSONArray 请求

    【讨论】:

    • 就在 onResponse 回调中。我部分复制了您的代码并添加了转换。
    • 我收到一个错误:类型 org.json.JSONArray 无法通过我更改的方式转换为 JSONObject:gson.fromJson(responsestr, listType);到这个 :gson.fromJson(responsenews.toString(), type);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2017-07-02
    • 2021-07-22
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多