简体   繁体   中英

Retrieve data from a json object with square brackets enclosing the data

I got this data that are am getting from the database using an xmlhttprequest.

[{"description":"welcome","location":"Nairobi","name":"Equity Bank","created_at":"2013-02-07 21:12:45"},{"description":"very nice job","location":"Kisumu","name":"Equity Bank","created_at":"2013-02-16 12:19:46"},{"description":"welcome all","location":"nairobi","name":"Equity Bank","created_at":"2013-02-16 13:28:26"},{"description":"Very nice","location":"Nairobi","name":"Equity Bank","created_at":"2013-07-08 01:50:11"},{"description":"bad job","location":"eldoret","name":"Safaricom","created_at":"2013-02-03 00:00:00"},{"description":"very nice job","location":"mombasa","name":"Deep Africa","created_at":"2013-02-05 00:00:00"}]

My main aim is to arrange the data in a column form as follows:

description      location     name   created_at  

The square brackets confuse me a little bit but it seems like a javascript array but i just cant parse it correctly.

You could just loop through it, and access each element. Here is an example formatting it into some HTML tables.

var data = [{"description":"welcome","location":"Nairobi","name":"Equity Bank","created_at":"2013-02-07 21:12:45"},{"description":"very nice job","location":"Kisumu","name":"Equity Bank","created_at":"2013-02-16 12:19:46"},{"description":"welcome all","location":"nairobi","name":"Equity Bank","created_at":"2013-02-16 13:28:26"},{"description":"Very nice","location":"Nairobi","name":"Equity Bank","created_at":"2013-07-08 01:50:11"},{"description":"bad job","location":"eldoret","name":"Safaricom","created_at":"2013-02-03 00:00:00"},{"description":"very nice job","location":"mombasa","name":"Deep Africa","created_at":"2013-02-05 00:00:00"}];


var results = document.getElementById('results');
htmlString = '<table><tr>';
for( a=0; a<data.length; a++) {
    htmlString = htmlString + '<tr><td>' + data[a]['description'] + '</td><td>' + data[a]['location'] + '</td><td>' + data[a]['name'] + '</td><td>' + data[a]['created_at'] +     '</tr>';

}
htmlString = htmlString + '</table>';
results.innerHTML = htmlString;

There's a jsfiddle at http://jsfiddle.net/R63BJ/

As If ur using a XMLHTTRequest you will get the data in responesText

 var jsonData=eval('('+responseText+')');

var text="<table><tr><th>description</th><th>location</th>" +
                "<th>name</th><th>created_at</th></tr>";
for(var i=0;i<jsonData.length;i++){


            text+="<td>"+jsonData[i].description+"</td>";
            text+="<td>"+jsonData[i].location+"</td>";
            text+="<td>"+jsonData[i].name+"</td>";
            text+="<td>"+jsonData[i].created_at+"</td>";
            text+="</tr>";

}
text+="</table>"
document.getElementById("tag").innerHTML="";// make sure that data is not Present 
    document.getElementById("tag").innerHTML=text;

and In the Jsp Page:

<div id="tag"></tag>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM