【问题标题】:how to parse the data from the database json Format [closed]如何从数据库中解析数据 json 格式 [关闭]
【发布时间】:2015-04-14 10:14:21
【问题描述】:

我想在 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。

【问题讨论】:

  • 在我深入您的字符串“链”之前:打印行打印的是什么?
  • ……你有可能混淆了listlistscan吗?
  • @Klaus D 我照你说的做了
  • 只使用返回不打印结果
  • 好的,但是 pb 没有打印出来我按照你说的做了,$("#donnees>table").html(lang); 的结果的 pb未定义

标签: javascript jquery python json


【解决方案1】:

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

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/

【讨论】:

  • 感谢 Guilherme Ferreira,但我没有创建我的列表,例如 Id 和 HOST_NAME 的值是可变的
猜你喜欢
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
相关资源
最近更新 更多