2015-04-14 77 views
1

我想在HTML頁面中顯示我的json的內容 我在數據庫中讀取我的表中的行我將它們放在一個列表中,然後我嘗試解析json格式,但問題母雞我儘量不通過exmple來檢索JSON我能對某些數據,如果我想顯示的名稱或類似的如何從數據庫json中解析數據格式

@APP.route('/scan',methods=['GET']) 
def scan(): 
    listscan = [] 
    con = mdb.connect('localhost','testuser', 'test623', 'bd') 
    cur = con.cursor() 
    cur.execute("SELECT * FROM tablesd") 
    rows = cur.fetchall() 
    for row in rows: 
     chaine = "Id:" + str(row[0]) + ',' + "HOST_NAME:" + str(row[1]) + "," + "ADDRESS:" + str(row[2]) + "," + "IP:" + str(row[3]) + "," + "TIME:" + str(row[4]) + "," + "DATE:" + str(row[5]) + "," + "CONN:" + str(row[6]) + "," + "ETAT:" + str(row[7]) + "," + "NEWgh:" + str(row[8]) 
    listscan.append(chaine) 

    print json.dumps(listscan) 
    return json.dumps(listscan) 
function scan(){ 
    $.ajax({ 
     type: 'GET', 
     url: '/scan', 
     dataType: 'json', 
     success: function(json) { 
      alert('ok'); 
      var lang = ''; 
      $.each(json, function() { 
       lang += this['Id'] + "<br/>"; 
      }); 
      $("#donnees").html(lang); 
     }, 
     error: function() { 
      alert('La requête n\'a pas abouti'); 
     } 
    });  
} 

打印json.dumps的結果的東西(listscan ):

[「Id:1,HOST_NAME:,ADDRESS:x:x:x:x:x:x,IP:172.xxx,TIME:17:25,DATE:04/04/15,CONN:0,ETAT :null,NEWgh:1「,」Id:2,HOST_NAME:,ADDRESS:Y:Y:Y:Y:Y:Y,IP:192.xxx,TIME:17:25,DATE:04/04/15, CONN:0,ETAT:null,NEWgh:1「]

我喜歡display undefined我還沒有收到id。

+0

之前,我潛入你的字符串「鏈」的存在:什麼是打印在打印行? –

+1

...有可能你混淆了'list'和'listscan'? –

+0

@克勞斯DI做了你所說的 – kaio

回答

1

您應該更新像您迴應(listscan):

var json = [ 
{'Id':1, HOST_NAME:'', ADDRESS:'x:x:x:x:x:x', IP:'172.x.x.x', TIME:'17:25', DATE:'04/04/15', CONN:0, ETAT:null, NEWgh:1}, 
{'Id':1, HOST_NAME:'', ADDRESS:'x:x:x:x:x:x', IP:'172.x.x.x', TIME:'17:25', DATE:'04/04/15', CONN:0, ETAT:null, NEWgh:1}, 
]; 

$.each(json, function() { 
    $('body').append(this['Id'] + '<br\>'); 
}); 

在這裏你可以找到一個例子:http://jsfiddle.net/guilhermeaferreira/jm86qr0v/

+0

感謝Guilherme Ferreira,但我不創建我的列表,例如Id和HOST_NAME的值是可變的 – kaio