Python:使用Flask在HTML中渲染/显示JSON对象

时间:2015-12-04 21:55:17

标签: python html json html5 jinja2

我正在使用python词典/列表并将其转换为JSON对象。我正在尝试用HTML呈现此JSON记录。我无法在html表中显示它。如果我以正确的方式访问html中的python字典,请告诉我?

代码段如下。

Python代码

cursor=db.tweets.find({"$text": {"$search": value}})

json_docs = [json.dumps(document , default=json_util.default) for document in cursor ]
print(json_docs)
for jsondump in json_docs:
    x=json.loads(jsondump)
    print(x)
titles = [title for title in json_docs]
return render_template("searchResult.html", titles=titles)

HTML code:

<table border="1" style="width:50%">
   {% for title in titles %}
    <tr>
        <td>
            {{ title }}
        </td>
        <td>
            <a href="comment">
                View Data / Comments
            </a>
        </td>
    </tr>
    {% endfor %}

</table>

json docs的示例数据

['{"source": "&lt;a href=&quot;http://twitter.com/download/iphone&quot;&gt;Twitter for iPhone&lt;/a&gt;", "createdAt": "Wed, 31 Oct 2012 02:05:13 +0000", "latitude": 42.495299, "longitude": -92.329888, "twitterTweetId": 2.63e+17, "fromUser": "PrettyboiDenzel", "LanguageCode": "en", "id": 136269, "text": "Aye low key that bitch #hurricanesandy is up by Wisconsin .... If I\'\'m thinking right ..... THAT\'\'S CLOSE ASS HELL! That bitch sandy CRAYYY!", "_id": {"$oid": "565de24b39b758efe19d5b4c"}, "fromUserId": 57104215, "profileImageUrl": "http://a0.twimg.com/profile_images/2747557265/de3a171c1262c5a84016e6304c8eedc6_normal.jpeg", "fromUserName": "PrettyBoi\'\'Denzel"}']

x的值

{'source': '&lt;a href=&quot;http://twitter.com/download/iphone&quot;&gt;Twitter for iPhone&lt;/a&gt;', 'latitude': 42.495299, 'longitude': -92.329888, 'twitterTweetId': 2.63e+17, 'fromUser': 'PrettyboiDenzel', 'LanguageCode': 'en', 'id': 136269, 'createdAt': 'Wed, 31 Oct 2012 02:05:13 +0000', '_id': {'$oid': '565de24b39b758efe19d5b4c'}, 'text': "Aye low key that bitch #hurricanesandy is up by Wisconsin .... If I''m thinking right ..... THAT''S CLOSE ASS HELL! That bitch sandy CRAYYY!", 'fromUserId': 57104215, 'profileImageUrl': 'http://a0.twimg.com/profile_images/2747557265/de3a171c1262c5a84016e6304c8eedc6_normal.jpeg', 'fromUserName': "PrettyBoi''Denzel"}

0 个答案:

没有答案