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. looping through json objects nice posts..
    sap netweaver training in hyderabad

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

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

    ReplyDelete
  9. 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
  10. Interesting blog JSON objects which attracted me more.Spend a worthful time.keep updating more.
    Digital marketing company in Chennai

    ReplyDelete
  11. 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
  12. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

JSP page directives

A jsp page directive looks like this: <%@ directive attribute="value" %> I am not gonna explain each and every page directives here . I would like to discuss about two page directives  which is import and include. First of all consider the import directive . The following simple program illustrate the use of import page directive: The output would be something looks like this: <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%>  <%@ page import="java.util.Date" %>   //page directive <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Jsp Basics</title> </head> <body> <%=new Date() %> </body> </html> Tue Nov 12 17:42:34 I...

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...