循环访问JSON字符串的Javascript

时间:2010-09-21 05:21:01

标签: javascript json

好的,JSON格式有点新......

我有一个从AJAX调用返回的以下JSON字符串,这个firebug实际上很好地显示在树中...但是我似乎无法弄清楚如何遍历内容......

{"data":{"item":[{"@id":"7","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"00:02:18"},{"@id":"8","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"08:26:24"}]}}

我试图说得到一些项目.. alert(data.item.length); 或循环:

for(i=0; i<data.item.length; i++)
{
    alert(data.item[i].FromMember);
}
显然缺少一些有趣的东西......

任何想法??

5 个答案:

答案 0 :(得分:2)

你非常接近......“数据”实际上是你JSON中的一个关键,所以你必须引用你的JSON变量来访问“数据”....所以你想要JSON.data.item[i].FromMember

以下是一些完整的工作代码:

(function () {
    var json = {"data":{"item":[{"@id":"7","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"00:02:18"},{"@id":"8","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"08:26:24"}]}};

    var i;
    var iLength = json.data.item.length;
    for (i = 0; i < iLength; i++) {
        alert(json.data.item[i].FromMember);
    }
})();​

jsFiddle

答案 1 :(得分:2)

JSON对象是新浏览器中的标准。对于旧版浏览器,您可以添加javascript库json2.js from json.org(缩小2.5kb)。

要将字符串转换为对象,请使用JSON.parse

var response = JSON.parse('{"data":{"ite...ime":"08:26:24"}]}}'),
    item = response.data.item;

要将数据发回服务器,请使用JSON.stringify

var jsonString = JSON.stringify(theObject);

答案 2 :(得分:0)

确保使用反序列化JSON。

var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);

取决于您使用的是哪个JavaScript框架,每个框架都有自己的反序列化器。

上面的示例使用的是MicrosoftAjax.js库。

更多信息here

答案 3 :(得分:0)

你应该有这样的东西才能发挥作用。注意obj中的for-loop

var obj = {"data":{"item":[{"@id":"7","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"00:02:18"},{"@id":"8","fromMemberID":"7","FromMember":"david","notificationsType":"event","notificationsDesc":"A new event (Test Event Thursday, 16 September 2010) has been created.","notificationsDate":"16 Sep 2010","notificationsTime":"08:26:24"}]}};

for (i = 0; i < obj.data.item.length; i++) {
    alert(obj.data.item[i].FromMember);
}​

或者如果您有data作为变量,仍然应将其称为data.data.item.length.

答案 4 :(得分:0)

将你的json传递给下面的函数。

function getData(obj) {
    var myData = obj.data.item, i, output = '';

    for (i = 0; i < myData.length; i += 1) {
        for (key in myData[i]) {
         output += key + " : " + myData[i][key];
        }       
    }
    return output;
}

Click for example