0

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"}]

私の主な目的は、次のようにデータを列形式で配置することです。

description      location     name   created_at  

角かっこは私を少し混乱させますが、JavaScript 配列のように見えますが、正しく解析できません。

4

2 に答える 2

1

それをループして、各要素にアクセスすることができます。これをいくつかの HTML テーブルにフォーマットする例を次に示します。

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;

http://jsfiddle.net/R63BJ/に jsfiddle があります。

于 2013-10-04T11:22:58.500 に答える
0

XMLHTTRequest を使用しているかのように、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;

およびJspページで:

<div id="tag"></tag>
于 2013-10-04T11:23:07.233 に答える