2016-12-27 78 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