【问题标题】:Retrieve data from a json object with square brackets enclosing the data从带有方括号的 json 对象中检索数据
【发布时间】:2013-10-11 08:33:03
【问题描述】:

我使用 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 数组,但我无法正确解析它。

【问题讨论】:

  • 使用哪种语言?我假设是 javascript?
  • 是的..它的javascript。这就是我得到它的方式..var datas=JSON.parse(internship.responseText); var 工作=JSON.stringify(数据);警报(工作);

标签: javascript json var


【解决方案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

【讨论】:

    【解决方案2】:

    如果您使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多