How to use jQuery .each() with json value


#1

I am reading an json file values but not i have to show the values in HTML table structure. I want to loop through the .each () method and then show the values. Please help me?


#2

looking at jquery docs:

http://api.jquery.com/jquery.each/

we are given the code:

var obj = {
  "flammable": "inflammable",
  "duh": "no duh"
};
$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});

we can use the same code for a json file.

i found some json data online, combined with the each documentation it was pretty easy:

http://jsbin.com/vupawofotu/edit?html,js,console


#3

jQuery each is used for looping. First use .getJSON to fech json file data like this:

$.getJSON("demo_ajax_json.js", function(result){
       $.each(result, function(i, field){
            //create table
        });
    });

Now inside the function(result){} call jQuery each method to loop through the data. Now to make a table of data use .append() method adding ‘tr’ & ‘td’ on every loop.

Basically I am giving you some ideas which you can apply and do this.


#4

thank you for the explanation.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.