gpt4 book ai didi

javascript - 如何从数据库中解析json格式的数据

转载 作者:行者123 更新时间:2023-12-03 10:21:07 26 4
gpt4 key购买 nike

我想在 HTML 页面中显示我的 json 内容我读取了数据库中表的行,将它们放入列表中,然后尝试解析 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.x.x.x,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.x.x.x,TIME:17:25,DATE:04/04/15,CONN:0,ETAT:null,NEWgh:1"]

我喜欢显示未定义,我还没有收到 ID。

最佳答案

您应该更新您的回复(列表扫描),例如:

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/

关于javascript - 如何从数据库中解析json格式的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com