循环通过Json对象

时间:2012-04-27 14:49:30

标签: html css json

我有以下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 个答案:

答案 0 :(得分: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] );
}

答案 1 :(得分:5)

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

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

jsFiddle JSON / jQuery example.

基本上,用$.each(json, function (index, item) { /*...*/ });

来处理这个难以理解的词

答案 2 :(得分:1)

上面提到的对象是......对象,而不是数组。唯一的数组是s.queue_details。你可以这样做来循环它:

for (var i = 0; i < s["queue_details"].length; i++)
{
  var item = s["queue_details"][i];
  //do stuff with item
}

答案 3 :(得分:0)

使用length属性(仅限数组)无法获取对象的长度。您可以使用for (x in y)自行遍历对象。