2015-09-05 59 views
0

现在JSON数据已存储在ajax中的变量“msg”中。我只能在页面中提醒(msg)。但我想把它放入数据表或以任何合适的方式查看ajax或js中的列的数据。将json转换为jQuery数据表中的ajax?

这里是JSON类型:

{ "aaData": [ { "ID": "1", "FESTIVAL": "Antipodes Festival", "SUBURB": "Lonsdale Street, Melbourne", "POSTCODE": "3000", "WEBSITE": "http://www.antipodesfestival.com.au/", "DESCRIPTION": "The greek precinct in melbourne cbd will transform into a huge, free street festival with the hosting of the antipodes lonsdale street festival which will hold sway from 14 february 2015 to 15 february 2015." }, { "ID": "5", "FESTIVAL": "Boite Singers Festival", "SUBURB": "Victoria", "POSTCODE": "3000", "WEBSITE": "http://boite.com.au/index.php", "DESCRIPTION": "The boite singers festival brings you four days of vocal inspiration and sheer fun on the second weekend of january each year." } ] } 

回答

0

我不明白你的问题,但我想你想作为一个表来显示你的JSON值!

$(document).ready(function(){ 
 
var myjson = { "aaData": [ { "ID": "1", "FESTIVAL": "Antipodes Festival", "SUBURB": "Lonsdale Street, Melbourne", "POSTCODE": "3000", "WEBSITE": "http://www.antipodesfestival.com.au/", "DESCRIPTION": "The greek precinct in melbourne cbd will transform into a huge, free street festival with the hosting of the antipodes lonsdale street festival which will hold sway from 14 february 2015 to 15 february 2015." }, { "ID": "5", "FESTIVAL": "Boite Singers Festival", "SUBURB": "Victoria", "POSTCODE": "3000", "WEBSITE": "http://boite.com.au/index.php", "DESCRIPTION": "The boite singers festival brings you four days of vocal inspiration and sheer fun on the second weekend of january each year." } ] } ; 
 
console.log(myjson); 
 
for(i=0;i < myjson.aaData.length;i++){ 
 
    var html=''; 
 
    $.each(myjson.aaData[i], function(ind,val){ 
 
     html +='<td>'+val+'</td>'; 
 
    }); 
 
    $('#table_id tbody').append('<tr>'+html+'</tr>'); 
 
} 
 
    $('#table_id').DataTable(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="http://cdn.datatables.net/1.10.9/css/jquery.dataTables.css" rel="stylesheet"/> 
 
<script src="http://cdn.datatables.net/1.10.9/js/jquery.dataTables.js"></script> 
 
<table id="table_id" class="display"> 
 
    <thead> 
 
     <tr> 
 
      <th>ID</th> 
 
      <th>FESTIVAL</th> 
 
      <th>SUBURB</th> 
 
      <th>POSTCODE</th> 
 
      <th>WEBSITE</th> 
 
      <th>DESCRIPTION</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody>   
 
    </tbody> 
 
</table>

我希望这将帮助你:)

+0

它给出了一个错误:0x800a138f - JavaScript运行时错误:无法获取未定义或空引用的属性“长度”。发生在myjson.aaData.length中。顺便说一句,我已经在页面中添加了参考,我不知道为什么。 –

+0

我试过你的代码,它的工作原理。唯一的问题是我的数据存储在变量“msg”中,每当我从sql server获取数据时它都会更改(但格式相同)。我不能var myjson = {...};我用msg.aaData.length,它会给上面的错误。你能告诉我如何解决它吗? –

+0

它在创建dataTable()时会产生错误。 –

0

这是这样

//html 
<table id="example" class="display" width="100%"> 
</table> 

//jquery 
$('#example').DataTable({ 
    "aaData": data, 
    "aoColumns": [ 
     { "mDataProp": "name" }, 
     { "mDataProp": "position" }, 
     { "mDataProp": "office" }, 
     { "mDataProp": "extn" }, 
     { "mDataProp": "start_date" }, 
     { "mDataProp": "salary" } 
    ] 
}); 
//data source 

var data= [ 
{ 
    "name": "Tiger Nixon", 
    "position": "System Architect", 
    "salary": "$320,800", 
    "start_date": "2011/04/25", 
    "office": "Edinburgh", 
    "extn": "5421" 
}, 
{ 
    "name": "Garrett Winters", 
    "position": "Accountant", 
    "salary": "$170,750", 
    "start_date": "2011/07/25", 
    "office": "Tokyo", 
    "extn": "8422" 
} 
] 

你应该参考this计算器问题后。和this小提琴

+0

对不起,我的json数据是从sql server实现的,我用一个变量“msg”来获取内容,它可以改变。但是,谢谢你的回答。 –