Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在JSON字符串中循环_Javascript_Json - Fatal编程技术网

Javascript在JSON字符串中循环

Javascript在JSON字符串中循环,javascript,json,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

好的,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"}]}}
我想说的是,去数一数物品。。警报(数据、项目、长度); 或循环:

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

for(i=0;i确保使用反序列化JSON

var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);
根据您使用的JavaScript框架,每个框架都有自己的反序列化程序

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


更多信息。

您应该有这样的东西才能工作。请注意
for循环中的
obj

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);
}​
var obj={“data”:{“item”:[{“@id”:“7”,“fromMemberID”:“7”,“FromMember”:“david”,“notificationsType”:“event”,“notificationsDesc”:“已创建了一个新事件(2010年9月16日星期四测试事件)”,“notificationsDate”:“2010年9月16日”,“notificationsTime”:“00:02:18”},{“@id”:“8”,“fromMemberID”:“7”,“FromMember”:“david”,“notificationsType”:“event”,“notificationsDesc”:“已创建一个新事件(2010年9月16日,星期四测试事件)。,“notificationsDate”:“2010年9月16日”,“notificationsTime”:“08:26:24”}]};
对于(i=0;i

或者,如果变量为
data
,则仍应将其称为
data.data.item.length。

只需将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;
}
函数getData(obj){ var myData=obj.data.item,i,output=''; 对于(i=0;i

您非常接近…”实际上,“数据”是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);
    }
})();​
(函数(){
var json={“data”:{“item”:[{“@id”:“7”,“fromMemberID”:“7”,“FromMember”:“david”,“notificationsType”:“event”,“notificationsDesc”:“已创建新事件(2010年9月16日星期四测试事件)”,“notificationsDate”:“2010年9月16日”,“notificationsTime”:“00:02:18”},{“@id”:“8”,“fromMemberID”:“7”,“FromMember”:“david”,“notificationsType”:“event”,“notificationsDesc”:“已创建一个新事件(2010年9月16日,星期四测试事件)”,“notificationsDate”:“2010年9月16日”,“notificationsTime”:“08:26:24”}]};
var i;
var iLength=json.data.item.length;
对于(i=0;i

JSON对象是新浏览器中的标准。对于旧浏览器,您可以添加javascript库(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);

你把JSON字符串转换成JavaScript对象了吗?@William,lol-应该有一个“新评论已经发布”的通知,就像回答一样。:)有时候很难决定是做评论还是回答谢谢!是的,看起来JSON数据是以字符串而不是对象的形式返回的..谢谢!是的,奇怪的事情..JSON数据本质上是字符串而不是对象!谢谢你的回答!