Craig Kuyper
Craig Kuyper

Reputation: 65

How can I populate a table with part of a JSON

I have a JSON being returned an I want to take the last 8 elements of the JSON and make a table. The table builds the way I expect it to but I'm getting undefined as the result value. Here is the code for the getJSON, an edited down return and the table.

$.getJSON("loadloads.php", function(data){
 [{
         "value": {
             "lineNumber": "258640",
             "TypeId": "1",
             "StopNumber": "1",
             "ReferenceNo": "0002325063",
             "LocationId": "3",
             "SLN": "227311",
             "LoName": "Elk GAF Materials Corp - Shafter",
             "Type": "Shipper"
         }
     }, {
         "value": {
             "lineNumber": "258641",
             "TypeId": "2",
             "StopNumber": "2",
             "ReferenceNo": "682383",
             "LocationId": "205697",
             "SLN": "227311",
             "LoName": "RWC Building Products - Albuquerque",
             "Type": "Consignee"
         }
     }
 ]

   var table_obj = $('table');
$.each(data, function(index,value){
     table_obj.append($('<tr><td>'+value.SLN+'</td><td >'+value.Type+'</td><td>'+value.StopNumber+'</td><td>'+value.LoName+'</td><td>'+value.ReferenceNo+'</td><td class="hide">'+value.TypeId+'</td><td class="hide">'+value.Locationid+'</td><td class="hide">'+value.lineNumber+'</td></tr>'));

Upvotes: 3

Views: 93

Answers (4)

Adil Shaikh
Adil Shaikh

Reputation: 44740

Try this way .

value.value.SLN

Upvotes: 2

monkeyinsight
monkeyinsight

Reputation: 4859

try this

for (var i in data)
    console.log(data[i].value);

if it will output in console result, just use it as data[i].value.field

Upvotes: 0

Anoop
Anoop

Reputation: 23208

Try this. All properties are inside value so you should try value.value

$.each(data, function(index, v){
    var value = v.value;
     table_obj.append($('<tr><td>'+value.SLN+'</td><td >'+value.Type+'</td><td>'+value.StopNumber+'</td><td>'+value.LoName+'</td><td>'+value.ReferenceNo+'</td><td class="hide">'+value.TypeId+'</td><td class="hide">'+value.Locationid+'</td><td class="hide">'+value.lineNumber+'</td></tr>'));
}

Upvotes: 2

Louis Ricci
Louis Ricci

Reputation: 21106

The name you gave your variable "value" is confusing you. You still need to reference the "value" key within your object that just happens to be named value.

'+value.value.SLN+

Upvotes: 3

Related Questions