Solved

How to loop through JSON array of objects

Posted on 2010-08-28
4
938 Views
Last Modified: 2012-05-10
Hello,

I need to loop through this array.. how can I do that?

var buildCarousel = '/components/ajaxproxy/mostactive.cfc?method=getmostactive&returnformat=json';
$.getJSON(buildCarousel, function(data) {

});
JSON array of objects.

[{"P":"\/photos\/family\/173000\/173420s.jpg","L":1121895},{"P":"\/photos\/family\/173000\/173422s.jpg","L":1121887},{"P":"\/photos\/family\/173000\/173005s.jpg","L":1121876}

Open in new window

0
Comment
Question by:Steggs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33550880
Use :


$.getJSON(buildCarousel, function(data) {
    for(i=0;i<data.length;i++) {
        alert(data[i].P);
        alert(data[i].L);
    }
});

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33550883
test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script language="javascript">
	var data = [{"P":"\/photos\/family\/173000\/173420s.jpg","L":1121895},{"P":"\/photos\/family\/173000\/173422s.jpg","L":1121887},{"P":"\/photos\/family\/173000\/173005s.jpg","L":1121876}];
//$.getJSON(buildCarousel, function(data) {
    for(i=0;i<data.length;i++) {
        alert(data[i].P);
        alert(data[i]["L"]);
    }
//});
</script>
</body>
</html>

Open in new window

0
 
LVL 30

Author Closing Comment

by:Steggs
ID: 33551665
Thanks :)

I was using the for loop but was incorrectly using:

data[i].L

instead of:

data[i]["L"]
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33551718
You're welcome! Thanks for he points!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question