Solved

How to loop through JSON array of objects

Posted on 2010-08-28
4
894 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
  • 3
4 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You're welcome! Thanks for he points!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now