最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript looping through a JSON string - Stack Overflow

programmeradmin3浏览0评论

Ok, a little new to JSON format..

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...

{"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:

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

obviously missing something fundemental...

Any ideas??

Ok, a little new to JSON format..

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...

{"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:

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

obviously missing something fundemental...

Any ideas??

Share Improve this question asked Sep 21, 2010 at 5:21 Dav.idDav.id 2,7973 gold badges45 silver badges57 bronze badges 3
  • Did you convert the JSON string to JavaScript object? – William Niu Commented Sep 21, 2010 at 5:23
  • @William, lol - there should be a "new ment has been posted" notification, like there is for answers. – RPM1984 Commented Sep 21, 2010 at 5:25
  • :) sometimes it's hard to decide whether to do a ment or an answer – William Niu Commented Sep 21, 2010 at 5:28
Add a ment  | 

5 Answers 5

Reset to default 5

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

To transform the string to an object, use 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:

var jsonString = JSON.stringify(theObject);

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

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

you should have something like this for it to work. notice the obj in the 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 data as your variable, still you should call it as data.data.item.length.

Make sure you use de-serialize the JSON.

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

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

The above example is using the MicrosoftAjax.js library.

More info here.

Just pass your json to the function below.

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

发布评论

评论列表(0)

  1. 暂无评论