2

我有以下 JSON 对象

var s = {
    "TenantGroupName": "Fun Holidays",
    "queue_details": [{
        "queue_key": "1",
        "channelId": "1",
        "queue_name": "North India Travel",
        "contacts": [{
            "from": "meenar@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "sagarm@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "kaushalk@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "kaushalk@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "8:05",
            "service_level_breached": "true",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "kaushalk@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "8:34",
            "service_level_breached": "true",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "1",
        "queue_name": "South India Travel",
        "contacts": []
    }, {
        "queue_key": "1",
        "channelId": "1",
        "queue_name": "Summer 2012",
        "contacts": [{
            "from": "meenar@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "raheels@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "sagarm@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "kaushalk@interactcrm.com",
            "subject": "Redemption Coupon",
            "queue_time": "9:34",
            "service_level_breached": "true",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "3",
        "queue_name": "Honeymoon",
        "contacts": [{
            "from": "Henry Williams",
            "subject": "New Zealand",
            "queue_time": "1:45",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }, {
            "from": "Linda Simpson",
            "subject": "Redemption Coupon",
            "queue_time": "2:34",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "7",
        "queue_name": "Feedback_SMS",
        "contacts": [{
            "from": "@sanjeev",
            "subject": "Maldives",
            "queue_time": "1:45",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }, {
        "queue_key": "1",
        "channelId": "5",
        "queue_name": "Feedback_Twitter",
        "contacts": [{
            "from": "9833202859",
            "subject": "Wish to travel ...",
            "queue_time": "1:45",
            "service_level_breached": "false",
            "cduId": "4f8b9f2322c60106c0a800bd65cc0000",
            "contactPkey": "8903"
        }]
    }]
}

我想遍历上面的 JSON 数组。我用 Javascript 编写了以下内容

我将上面的 JSON 对象传递给 Javascript,如下所示

function CreateQueue(jString) {
    var jSonString = { "MyQueue": jString };
    alert(jSonString.MyQueue.length);
}

上述功能提醒:undefined。如何遍历上面的 JSON 字符串?

4

4 回答 4

9

这整个构造是一个对象,而不是一个数组,所以你不能调用.length它。另外我不明白你为什么将它包装到一个额外的对象中,这绝对没有用。

要遍历对象的所有属性,请执行以下操作:

for (var key in jString) {
   if (jString.hasOwnProperty(key) ){
       console.log( "key:"+key+", val:"+jString[key] );
   }
}

要遍历数组,请执行以下操作:

for (var i = 0; i < s.queue_details.length; i++){
  console.log( i+":"+s.queue_details[i] );
}
于 2012-04-27T14:57:56.087 回答
5

我发现在客户端使用 json 和 XML 解析/迭代的最简单方法是在脚本中包含 jQuery。

我为您创建了一个小示例,演示循环遍历第一个队列的联系人条目并为每个条目生成格式化的链接输出。

jsFiddle JSON / jQuery 示例。

本质上,难词被照顾到
$.each(json, function (index, item) { /*...*/ });

于 2012-04-27T15:18:21.680 回答
1

The above object is...well..an object, not an array. The only array is s.queue_details. You can do this to loop through it:

for (var i = 0; i < s["queue_details"].length; i++)
{
  var item = s["queue_details"][i];
  //do stuff with item
}
于 2012-04-27T14:54:16.433 回答
0

You can't get the length of an object using the length property, only of an array. You can loop through the object yourself with a for (x in y).

于 2012-04-27T14:54:21.417 回答