我在理解jsonify
的工作原理時遇到了一些麻煩,即使我閱讀了文檔。正如你可以在下面看到的,我打電話給返回字典的lookup()
函數,然後我試圖對它進行jsonify。燒瓶 - 正確使用jsonify
@app.route("/articles")
def articles():
a = lookup(33496)
return jsonify([link=a["link"], title = a["title"]]) #invalid syntax error
我helpers.py
:
import feedparser
import urllib.parse
def lookup(geo):
"""Looks up articles for geo.""" #this function already parses the 'link' and 'title' form rss feed
# check cache for geo
if geo in lookup.cache:
return lookup.cache[geo]
# get feed from Google
feed = feedparser.parse("http://news.google.com/news?geo={}&output=rss".format(urllib.parse.quote(geo, safe="")))
# if no items in feed, get feed from Onion
if not feed["items"]:
feed = feedparser.parse("http://www.theonion.com/feeds/rss")
# cache results
lookup.cache[geo] = [{"link": item["link"], "title": item["title"]} for item in feed["items"]]
# return results
return lookup.cache[geo]
# initialize cache
lookup.cache = {}
是我得到的錯誤是無效的語法。任何想法到我做錯了什麼?謝謝
然後,如果您需要明確地轉換爲json,那麼jsonify的目的是什麼? – tadm123
是@Jari,問題是我已經使用'jsonify()'指出了它的編輯方式,但是我收到了一些語法錯誤。 – tadm123
我現在嘗試使用你的第一個例子,我也得到了錯誤。嗯 – tadm123