如何解析这个 JSON 数组?我想解析评论列表数组[重复]

Posted

技术标签:

【中文标题】如何解析这个 JSON 数组?我想解析评论列表数组[重复]【英文标题】:How to parse this JSON Array? I want to parse comment list array [duplicate] 【发布时间】:2018-07-22 06:31:51 【问题描述】:
"status":"True","message":"Comment List !!!","data":"total_comment":"33","comment_list":["id":"1","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-05","comm_time":"04:27:15","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic_other_users.png","comment":"good article","id":"2","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-04","comm_time":"00:00:00","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic.png","comment":"i like the article","id":"3","userid":"1","user_name":"Neeraj","comm_date":"2018-02-04","comm_time":"09:12:30","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic_other_users.png","comment":"i like the article","id":"4","userid":"1","user_name":"Neeraj","comm_date":"2018-02-04","comm_time":"00:00:00","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic.png","comment":"good","id":"5","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-04","comm_time":"07:39:29","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic.png","comment":"good","id":"6","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-04","comm_time":"00:00:00","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic_other_users.png","comment":"good","id":"7","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-06","comm_time":"07:24:06","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic.png","comment":"good","id":"8","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-06","comm_time":"00:00:00","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic_other_users.png","comment":"good","id":"9","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-06","comm_time":"00:00:00","userimage":"http:\/\/pht.org.in\/uploaded\/profile_pic_other_users.png","comment":"good","id":"10","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:22:46","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"i like the article","id":"11","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:22:58","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"i like the article","id":"12","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:31:43","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"good article","id":"13","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:36:31","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"","id":"14","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:38:51","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"","id":"15","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:40:14","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"good","id":"16","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:40:58","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"","id":"17","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:41:48","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"awesome","id":"18","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:42:37","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"","id":"19","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:45:58","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"","id":"20","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:48:11","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"","id":"21","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"06:51:18","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"I got it ","id":"22","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:14:04","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"I got it ","id":"23","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:27:26","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"geghdhd","id":"24","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:36:03","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"gsjdjxmmd","id":"25","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:40:52","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"gshsbsn","id":"26","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:44:59","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"hdjdxjkd","id":"27","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:48:29","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"xnndmd","id":"28","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:48:37","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"xnndmd","id":"29","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:54:03","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"gahzjxjx","id":"30","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"07:56:42","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"bsnsndn","id":"31","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"09:03:58","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"bznznzn","id":"32","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"23:06:44","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"good","id":"33","userid":"2","user_name":"Saurabh Nanda","comm_date":"2018-02-11","comm_time":"23:07:17","userimage":"http:\/\/pht.org.in\/uploaded\/","comment":"good"]

【问题讨论】:

使用json2java.azurewebsites.net 先搜索试试。 【参考方案1】:

试试这个;

try

    String status = response.getString("status");
    String message = response.getString("message");

    if (status.equals("True")) 
        JSONObject data_obj = response.getJSONObject("data");

        JSONArray comment_list_arr=data_obj.getJSONArray("comment_list");


        for(int i=0;i<comment_list_arr.length();i++)
        
            JSONObject obj=comment_list_arr.getJSONObject(i);

            String id=obj.getString("id");
            String userid=obj.getString("userid");
            String user_name=obj.getString("user_name");
            String comm_date=obj.getString("comm_date");
            String comm_time=obj.getString("comm_time");
            String userimage=obj.getString("userimage");
            String comment=obj.getString("comment");
            Log.d(TAG, "id: " + id+" userid:"+userid+" user_name:"+user_name);                                    
        
    
 catch (JSONException e) 
    Log.e(TAG, "unexpected JSON exception", e);

【讨论】:

【参考方案2】:

要获取状态,可以使用消息

 JsonObject obj = new JsonObject(your_json_string);
    obj.getString("status");

要获取 totalComment 和 comment_list,您可以使用:

JsonObject another_obj = obj.getJSONObject("data");
another_obj.getString("total_comment");

并获取您可以使用的所有 id 和其他东西:

JsonArray array_obj = another_obj.getJsonArray("comment_list");
array_obj.getStrting("id");

【讨论】:

以上是关于如何解析这个 JSON 数组?我想解析评论列表数组[重复]的主要内容,如果未能解决你的问题,请参考以下文章

解析从 c# 返回的 json 字符串数组

如何使用 C++ 解析 json 数组?

如何使用 Newtonsoft.Json 将包含数组数组的 json 对象解析为 C# 中的对象列表?

使用 Spray-json 解析简单数组

scala spray-json 如何解析一个复杂的 数组JSON

为啥 Javascript 不能从字符串文字中解析这个 JSON 数组?