Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

jquery code to display json response to a html table using append

Can some one help me out with code to display the json data in html table

 $.getJSON("http://10.0.2.2:8080/v1/service/1",
  function(data) {

    $.each(data, function(id, obj){

      });
});


<body>
   <table id="display">
   </table>
</body>

I want to display the json data in display table

json response data:

[
    {
        "firstcolumn":"56036",
        "loc":"Deli",
        "lastA":"Activity",
        "mTime":"2011-02-01 11:59:26.243",
        "nTime":"2011-02-01 10:57:02.0",
        "Time":"2011-02-01 10:57:02.0",
        "Age":"9867 Hour(s)",
        "ction":"                                                  ",
        "nTime":null
    },
    {
        "firstcolumn":"56036",
        "loc":"Deli",
        "lastA":"Activity",
        "mTime":"2011-02-01 11:59:26.243",
        "nTime":"2011-02-01 10:57:02.0",
        "Time":"2011-02-01 10:57:02.0",
        "Age":"9867 Hour(s)",
        "ction":"                                                  ",
        "nTime":null
    },
    {
        "firstcolumn":"56036",
        "loc":"Deli",
        "lastA":"Activity",
        "mTime":"2011-02-01 11:59:26.243",
        "nTime":"2011-02-01 10:57:02.0",
        "Time":"2011-02-01 10:57:02.0",
        "Age":"9867 Hour(s)",
        "ction":"                                                  ",
        "nTime":null
    },
    {
        "firstcolumn":"56036",
        "loc":"Deli",
        "lastA":"Activity",
        "mTime":"2011-02-01 11:59:26.243",
        "nTime":"2011-02-01 10:57:02.0",
        "Time":"2011-02-01 10:57:02.0",
        "Age":"9867 Hour(s)",
        "ction":"                                                  ",
        "nTime":null
    }
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You didn't give more information but anyway, If your json (data) structure is something like this

{
  "key_one": "value_one",
  "key_two": "value_two",
  "key_three": "value_three"
}

then you can do in your callback function

$.each(data, function(key, val) {
    $('<tr><td>ID: '+key+'</td><td id="'+key+'">'+val+'</td><tr>').appendTo('#display');
});

This will make a table like this example. hope it'll help you to get done your work.

Update

function(data) {
    $.each(data, function(key, val) {
        var tr=$('<tr></tr>');
        $.each(val, function(k, v){
            $('<td>'+v+'</td>').appendTo(tr);
        });
        tr.appendTo('#display');
    });?
});?

Here is an example.

Your full getJSON

$.getJSON("http://10.0.2.2:8080/v1/service/1",
    function(data) {
        $.each(data, function(key, val) {
            var tr=$('<tr></tr>');
            $.each(val, function(k, v){
                $('<td>'+v+'</td>').appendTo(tr);
            });
            tr.appendTo('#display');
        });?
    });?
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...