7

フラスコを使用して、simplejson の有無にかかわらず (適切なヘッダーを使用して) JSON をブラウザーに返したいと考えています。

@app.route('/')
def hello_world():
    QUERY_URL="http://someappserver:9902/myjsonservlet"
    result = simplejson.load(urllib.urlopen(QUERY_URL))
    return result;

返される JSON 出力が次のようであると仮定します。

{"myapplication":{"system_memory":21026160640.0,"percent_memory":0.34,
"total_queue_memory":4744,"consumers":1,"messages_unacknowledged":0,
"total_messages":0,"connections":1}

http://localhost:5000ただし、ページにアクセスすると、 Internal Server Error. 「結果」を適切に表示するにはどうすればよいですか? jsonまたは、ヘッダーを返すように指示できる方法はありますか?

結果を印刷するために print ステートメントを追加すると、JSON が表示されますが、ブラウザーではInternal Server Error.

4

1 に答える 1

10
import requests
r = requests.get(QUERY_URL)
return r.json

#normal return
return jsonify(username=g.user.username,
               email=g.user.email,
               id=g.user.id)

jsonify is available in flask. Here is the docs

于 2012-08-08T18:06:00.273 に答える