Skip to main content

How to loop through json objects using Jquery or javascript

These days handling json object is very essential in all kinds of applications , this post would help you to do that in very easy way .There are many way to handle json and some of them are described bellow .My aim is to help around the newbees here in json and javascript .


following code snippet will help you to call a remote end point and loop through the response json using jquery

Using Jquery


$.getJSON(requestURL, {
'apikey' : apiKey,
if (data.results && data.results.length > 0) {

$.each(data.results, function(i, rep) {
// do looping stuff here - i=index and rep is value
});

} else {
// else part
}
});


'zip' : zipcode,
}, function(data){

in pure java script we can loop through json object in two ways. If we are not concern about the order in which loop through we can go for a for each loop . Some other situations we must need to keep the order or we mus loop in sequential order in that case we must use standard for loop as given bellow


var info = {
"full_name" : "sudeep cv",
"title" : "Java developer",
"links" : {
    "blog"     : "http://iviewsource.com",
    "facebook" : "https://www.facebook.com/sudeepcv90",
"twitter"  : "https://twitter.com/sudeepcv",
"github"  : "https://github.com/sudeepcv",
"web"  : "http://sudeep-cv.tk/"
}
};

var output = "";

for ( key in info.links ) {
if (info.links.hasOwnProperty(key)) {

console.log( info.links[key] );

} //if the links has the key property
} // for...go through each link


or


for (var i = 0; i <= info.links.length; i++) {
for (key in info.links[i]) {
if (info.links[i].hasOwnProperty(key)) {

console.log( info.links[i][key] );

} // hasOwnProperty check
} // for each object
} //for each array element



Comments

  1. Everything is fine, am happy about your blog. Thanks admin for sharing the unique content, you have done a great job I appreciate your effort and I hope you will get more positive comments from the web users.

    Regards,
    Camellia
    SAP Training in Chennai

    ReplyDelete
  2. Another great information,excellent way for this one.coding easily understand and develop more skills after refer that post,thanks for sharing this post.
    html5 training in chennai

    ReplyDelete
  3. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.

    digital marketing training Chennai

    ReplyDelete
  4. this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    Digital Marketing Company in Chennai
    Digital Marketing Services in Chennai

    ReplyDelete
  5. Your content is awesome . You have done a great job and its very useful for me . I appreciate your effort and I hope that you will get more positive comments from the web users.
    Branding Services in Chennai

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. thanks for publishing this programming i getting idea,
    Back to original

    ReplyDelete
  8. Thank you for sharing the information here. Its much informative and really i got some valid information. You had posted the amazing article.

    Dataware Housing Training in Chennai

    ReplyDelete
  9. Interesting blog JSON objects which attracted me more.Spend a worthful time.keep updating more.
    Digital marketing company in Chennai

    ReplyDelete
  10. wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much
    Self Employment Tax
    Tax Preparation Services
    Tax Accountant
    Tax Consultant
    Tax Advisor

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete

Post a Comment