Solved

How to loop through JSON array of objects

Posted on 2010-08-28
4
935 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Read text on Table 7 46
web page freezes after ajax post 7 95
how to use string functions to get particular string 3 50
javascript delete path 7 42
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

738 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