使用jquery从json响应中获取特定数组的计数

时间:2015-09-02 02:46:05

标签: jquery json

我有这个JSON数组:

{
 "error":"false",
   "result":{
      "active_chats":{
         "list":[

         ]
      },
      "closed_chats":{
         "list":[

         ]
      },
      "pending_chats":{
         "list":[
            {
               "id":"90",
               "nick":"reee",
               "referrer":"\/\/xyz.com\/",
               "dep_id":"1",
               "time_created_front":"06:26:09",
               "department_name":"IT",
               "wait_time_pending":"04 m. 44 s.",
               "wait_time_seconds":284
            },
            {
               "id":"89",
               "nick":"admin",
               "referrer":"\/\/xyz.com\/",
               "dep_id":"1",
               "user_closed_ts":"1441160928",
               "time_created_front":"06:25:08",
               "department_name":"IT",
               "wait_time_pending":"05 m. 45 s.",
               "wait_time_seconds":345
            }
         ],
         "nick":"reee | IT",
         "msg":"dfgfg",
         "last_id_identifier":"pending_chat",
         "last_id":90
      },
      "transfer_chats":{
         "list":[

         ],
         "last_id_identifier":"transfer_chat",
         "last_id":0
      },
      "transfer_dep_chats":{
         "list":[

         ],
         "last_id_identifier":"transfer_chat",
         "last_id":0
      },
      "online_op":{
         "list":[
            {
               "id":"1",
               "user_id":"1",
               "dep_id":"0",
               "hide_online":"0",
               "last_activity":"1441161046",
               "last_accepted":"0",
               "active_chats":"0",
               "user":{
                  "id":"1",
                  "username":null,
                  "password":null,
                  "email":null,
                  "name":"ssseas Chat",
                  "filepath":null,
                  "filename":null,
                  "surname":"software",
                  "job_title":null,
                  "skype":null,
                  "xmpp_username":null,
                  "disabled":"0",
                  "hide_online":"0",
                  "all_departments":"1",
                  "invisible_mode":"0",
                  "time_zone":"Indian\/Mahe",
                  "rec_per_req":"0",
                  "session_id":"",
                  "lastactivity_ago":"7 s.",
                  "lastactivity":1441161046
               }
            }
         ]
      },
      "unread_chats":{
         "msg":"New unread message",
         "nick":"Visitor",
         "last_id":0,
         "last_id_identifier":"unread_chat",
         "list":[

         ]
      }
   }
}

我想使用jquery查询计算总待处理的聊天数。

我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

如果您的数组存储在变量中,则可以执行此操作

var data = {
   ...
}

alert(data.result.pending_chats.list.length)

访问对象时,可以使用点语法访问属性。

.length将返回list array

中的项目数

Here's a fiddle

答案 1 :(得分:0)

这很简单

var pjson = yourjson;
alert(pjson.result.pending_chats.list.length);

如果您的json不是格式良好,您可以使用以下方法作为安全检查。

var pjson = jQuery.parseJSON( 'yourjson' );