Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to loop through JSON array of objects

Posted on 2010-08-28
4
Medium Priority
?
944 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:Mark Steggles
[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 2000 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:Mark Steggles
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

636 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