Solved

Loop JSON Properties

Posted on 2009-07-02
3
2,397 Views
Last Modified: 2013-11-08
Hello Experts -

I am getting a JSON object using JQuery and .getJSON function.  

Is there a way to loop through object's properties and retrieve the name of each property?

I'm new to JSON, so an examples would be great.

Thanks.
0
Comment
Question by:soapygus
3 Comments
 
LVL 2

Accepted Solution

by:
iarla earned 500 total points
Comment Utility
Hi there

Have a look at the following link

http://stackoverflow.com/questions/684672/loop-through-json-object
0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
Comment Utility
You can try $.each() for this.

var students = {student1:{name:'Mr A', fathername:'Father A'},student2:{name:'Mr B', fathername:'Father B'},student3:{name:'Mr C', fathername:'Father C'}};

$(function(){

jQuery.each(students, function(i, val) {

      alert(i+' name: '+val.name);

    });

});

Open in new window

0
 

Author Closing Comment

by:soapygus
Comment Utility
Here's what worked in my solution (where 'data' is the JSON object)
 for (var key in data) {
                                if (data.hasOwnProperty(key)) {
                                    $('#findMe').append(key + '');
                                }
                            }
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 how to dynamically set the form action using jQuery.
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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now