Javascript 循环遍历 JSON 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3757495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:09:23  来源:igfitidea点击:

Javascript looping through a JSON string

javascriptjson

提问by Dav.id

Ok, a little new to JSON format..

好的,JSON 格式有点新。

I have the following JSON string returned from an AJAX call, which firebug actually displays in a tree quite nicely.. however I can't seem to be able to work out how to loop through the content...

我从 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"}]}}

I have tried to say get a count of items.. alert(data.item.length); or a loop:

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

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

obviously missing something fundemental...

显然缺少一些基本的东西......

Any ideas??

有任何想法吗??

采纳答案by Peter Ajtai

You were very close... "data" is actually a key in your JSON, so you have to refer to your JSON variable to access "data".... so you want JSON.data.item[i].FromMember

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

Here is some full working code:

这是一些完整的工作代码:

(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

js小提琴

回答by Mic

The JSON object is a standard in the new browsers. For older browsers you can add the javascript library json2.js from json.org(2.5kb minified).

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

To transform the string to an object, use JSON.parse

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

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

And to send back your data to the server, use JSON.stringify:

并将您的数据发送回服务器,请使用JSON.stringify

var jsonString = JSON.stringify(theObject);

回答by RPM1984

Make sure you use de-serialize the JSON.

确保使用反序列化 JSON。

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

Depends on which JavaScript frameworks you are using, each has it's own de-serializer.

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

The above example is using the MicrosoftAjax.js library.

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

More info here.

更多信息在这里

回答by Reigel

you should have something like this for it to work. notice the objin the for-loop.

你应该有这样的东西才能工作。注意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);
}?

or if you have dataas your variable, still you should call it as data.data.item.length.

或者如果你有data你的变量,你仍然应该把它称为data.data.item.length.

回答by Q_Mlilo

Just pass your json to the function below.

只需将您的 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

点击示例