2016-12-27 77 views
1

我从数据库中选择一个排它有一些JSON内容解析JSON成HTML模板

我的功能选择行

@route('/list') 
def list(): 
    c = conn.cursor() 
    c.execute("SELECT content FROM pages WHERE URL = 'blob.com'") 
    select = c.fetchall() 
    return template('views/list.tpl', rows=select) 

和行“内容”的结果是:

{ "ruleGroups": { 
    "SPEED": { "score": 97 } 
       }, 
    "pageStats": { "numberResources": 3, 
        "numberHosts": 2, 
        "totalRequestBytes": "500", 
        "imageResponseBytes": "3148", 
        "otherResponseBytes": "3838" 
       }, 
    "screenshot": { "mime_type": "image/jpeg", 
        "data": "imgcontent", 
        "width": 320, 
        "height": 240, 
        } 
} 

我想在json“score”,“numberResources”和“numberHosts”的list.tpl中显示。

我应该如何继续list.tpl?

+1

我不知道我的理解,也许是:'内容[ 'ruleGroups'] [ 'SPEED'] [ '分数']'; '内容[ 'pageStats'] [ 'numberResources']'; '内容[ 'pageStats'] [ 'numberHosts']'? – erasmortg

回答

0

在模板做如下因素:

% for row in rows: 
     % for key, value in row.iteritems(): 
      %if key == 'ruleGroups': 
       <p>SCORE: {{value['SPEED']['score']}}</p> 
      %elif key == 'pageStats': 
       <p>Number of resources: {{value['numberResources']}}</p> 
       <p>Number of hosts: {{value['numberHosts']}}</p> 
      %end 
     %end 
    %end