【问题标题】:Looping over json object in flask循环烧瓶中的json对象
【发布时间】:2014-12-17 19:45:34
【问题描述】:
@app.route('/parseJSON',methods=['GET','POST'])
def parseJSON():
    input_name = request.form["search"]
    url = "https://api.github.com/search/users?q={0}".format(input_name)
    loadurl = urllib.urlopen(url)
    data = json.loads(loadurl.read().decode(loadurl.info().getparam('charset') or 'utf-8'))
    item=data["items"][0]["avatar_url"]
    return item

这只是吐出很棒的 avatar_url。但现在我想显示用户名以及指向个人资料的链接。我认为这可以以某种方式通过循环来完成,但我对 Python/Flask 仍然很陌生,并且不确定如何做到这一点。最终输出应该如下

Username: "login"
Profile: "url"
img: "avatar_url" 

这应该会更新有人在搜索表单中输入新值的所有内容

@app.route('/search',methods=['GET','POST'])
def search():
    return render_template("search.html")

<form action="/parseJSON" method="POST">
    <input type="text" name="search">
    <input type="submit">
</form>

我打算将 avatar_url 放入一个 html img 标签并显示出来。

【问题讨论】:

  • 大概这是 Python 3 吧? getparam() 采用默认值,顺便说一下,您可以使用 get_charset()
  • 这里的预期结果是什么?网址列表?应该如何返回给请求者?作为一根长串?
  • 那么你根本不需要解码响应。
  • 那我该怎么做呢?
  • 我已经发布了答案。你能告诉我们你期待什么输出吗?这里应该向浏览器返回什么?

标签: python json api loops flask


【解决方案1】:

您可以使用列表理解提取所有个网址:

items = [item['avatar_url'] for item in data["items"]]

但在 Flask 中,您确实需要返回有效响应。通常这意味着您返回一个字符串,因此您需要加入这些 URL:

return '\n'.join(items)

您还可以返回 JSON 列表:

from flask import jsonify

# ....

return jsonify(items=items)

这将返回一个 JSON 对象,其键 'items' 指向列表; jsonify() function 会创建一个正确的 Response() 对象,并为您设置正确的 application/json 标头。

在 Python 2 中,您不需要解码 JSON 响应; GitHub 坚持 JSON RFC 并返回 UTF 编码的数据:

@app.route('/parseJSON',methods=['GET','POST'])
def parseJSON():
    input_name = request.form["search"]
    url = "https://api.github.com/search/users?q={0}".format(input_name)
    loadurl = urllib.urlopen(url)
    data = json.load(loadurl)
    items = [item['avatar_url'] for item in data["items"]]
    return '\n'.join(items)

演示:

>>> import urllib
>>> import json
>>> url = "https://api.github.com/search/users?q=martijn"
>>> loadurl = urllib.urlopen(url)
>>> data = json.load(loadurl)
>>> items = [item['avatar_url'] for item in data["items"]]
>>> print '\n'.join(items)
https://avatars.githubusercontent.com/u/107915?v=3
https://avatars.githubusercontent.com/u/623509?v=3
https://avatars.githubusercontent.com/u/431452?v=3
https://avatars.githubusercontent.com/u/46626?v=3
https://avatars.githubusercontent.com/u/46775?v=3
https://avatars.githubusercontent.com/u/180840?v=3
https://avatars.githubusercontent.com/u/245275?v=3
https://avatars.githubusercontent.com/u/670951?v=3
https://avatars.githubusercontent.com/u/121401?v=3
https://avatars.githubusercontent.com/u/506862?v=3
https://avatars.githubusercontent.com/u/627350?v=3
https://avatars.githubusercontent.com/u/2605679?v=3
https://avatars.githubusercontent.com/u/4040870?v=3
https://avatars.githubusercontent.com/u/120452?v=3
https://avatars.githubusercontent.com/u/167455?v=3
https://avatars.githubusercontent.com/u/965129?v=3
https://avatars.githubusercontent.com/u/515239?v=3
https://avatars.githubusercontent.com/u/197477?v=3
https://avatars.githubusercontent.com/u/178230?v=3
https://avatars.githubusercontent.com/u/490579?v=3
https://avatars.githubusercontent.com/u/1426964?v=3
https://avatars.githubusercontent.com/u/327472?v=3
https://avatars.githubusercontent.com/u/193881?v=3
https://avatars.githubusercontent.com/u/907436?v=3
https://avatars.githubusercontent.com/u/6215449?v=3
https://avatars.githubusercontent.com/u/580421?v=3
https://avatars.githubusercontent.com/u/3951973?v=3
https://avatars.githubusercontent.com/u/426811?v=3
https://avatars.githubusercontent.com/u/1290310?v=3
https://avatars.githubusercontent.com/u/1652861?v=3

如果您想将更多信息提取到字符串中,我会使用string formatting

template = '''\
Username: "{i[login]}"
Profile: "{i[url]}"
img: "{i[avatar_url]}"
'''
items = [template.format(i=item) for item in data["items"]]
return '\n'.join(items)

每个{} 占位符从使用i 关键字参数传入的项目中提取不同的键。

使用相同搜索的演示:

>>> template = '''\
... Username: "{i[login]}"
... Profile: "{i[url]}"
... img: "{i[avatar_url]}"
... '''
>>> items = [template.format(i=item) for item in data["items"]]
>>> print '\n'.join(items)
Username: "martijn"
Profile: "https://api.github.com/users/martijn"
img: "https://avatars.githubusercontent.com/u/107915?v=3"

Username: "martijnvermaat"
Profile: "https://api.github.com/users/martijnvermaat"
img: "https://avatars.githubusercontent.com/u/623509?v=3"

Username: "mvdkleijn"
Profile: "https://api.github.com/users/mvdkleijn"
img: "https://avatars.githubusercontent.com/u/431452?v=3"

Username: "dashorst"
Profile: "https://api.github.com/users/dashorst"
img: "https://avatars.githubusercontent.com/u/46626?v=3"

Username: "mjpieters"
Profile: "https://api.github.com/users/mjpieters"
img: "https://avatars.githubusercontent.com/u/46775?v=3"

Username: "karianna"
Profile: "https://api.github.com/users/karianna"
img: "https://avatars.githubusercontent.com/u/180840?v=3"

Username: "Mpdreamz"
Profile: "https://api.github.com/users/Mpdreamz"
img: "https://avatars.githubusercontent.com/u/245275?v=3"

Username: "Swaagie"
Profile: "https://api.github.com/users/Swaagie"
img: "https://avatars.githubusercontent.com/u/670951?v=3"

Username: "maerteijn"
Profile: "https://api.github.com/users/maerteijn"
img: "https://avatars.githubusercontent.com/u/121401?v=3"

Username: "MartijnB"
Profile: "https://api.github.com/users/MartijnB"
img: "https://avatars.githubusercontent.com/u/506862?v=3"

Username: "MartijnR"
Profile: "https://api.github.com/users/MartijnR"
img: "https://avatars.githubusercontent.com/u/627350?v=3"

Username: "Speedy1985"
Profile: "https://api.github.com/users/Speedy1985"
img: "https://avatars.githubusercontent.com/u/2605679?v=3"

Username: "Azeirah"
Profile: "https://api.github.com/users/Azeirah"
img: "https://avatars.githubusercontent.com/u/4040870?v=3"

Username: "mvexel"
Profile: "https://api.github.com/users/mvexel"
img: "https://avatars.githubusercontent.com/u/120452?v=3"

Username: "martijnboland"
Profile: "https://api.github.com/users/martijnboland"
img: "https://avatars.githubusercontent.com/u/167455?v=3"

Username: "Martijnc"
Profile: "https://api.github.com/users/Martijnc"
img: "https://avatars.githubusercontent.com/u/965129?v=3"

Username: "Pixelstudio"
Profile: "https://api.github.com/users/Pixelstudio"
img: "https://avatars.githubusercontent.com/u/515239?v=3"

Username: "mvmaasakkers"
Profile: "https://api.github.com/users/mvmaasakkers"
img: "https://avatars.githubusercontent.com/u/197477?v=3"

Username: "martijndeh"
Profile: "https://api.github.com/users/martijndeh"
img: "https://avatars.githubusercontent.com/u/178230?v=3"

Username: "Zegnat"
Profile: "https://api.github.com/users/Zegnat"
img: "https://avatars.githubusercontent.com/u/490579?v=3"

Username: "mgussekloo"
Profile: "https://api.github.com/users/mgussekloo"
img: "https://avatars.githubusercontent.com/u/1426964?v=3"

Username: "faassen"
Profile: "https://api.github.com/users/faassen"
img: "https://avatars.githubusercontent.com/u/327472?v=3"

Username: "martijnthe"
Profile: "https://api.github.com/users/martijnthe"
img: "https://avatars.githubusercontent.com/u/193881?v=3"

Username: "MartijnKaijser"
Profile: "https://api.github.com/users/MartijnKaijser"
img: "https://avatars.githubusercontent.com/u/907436?v=3"

Username: "ByteHazard"
Profile: "https://api.github.com/users/ByteHazard"
img: "https://avatars.githubusercontent.com/u/6215449?v=3"

Username: "martijnvg"
Profile: "https://api.github.com/users/martijnvg"
img: "https://avatars.githubusercontent.com/u/580421?v=3"

Username: "MartijnWoudstra"
Profile: "https://api.github.com/users/MartijnWoudstra"
img: "https://avatars.githubusercontent.com/u/3951973?v=3"

Username: "MartijnDwars"
Profile: "https://api.github.com/users/MartijnDwars"
img: "https://avatars.githubusercontent.com/u/426811?v=3"

Username: "cornips"
Profile: "https://api.github.com/users/cornips"
img: "https://avatars.githubusercontent.com/u/1290310?v=3"

Username: "martijn94"
Profile: "https://api.github.com/users/martijn94"
img: "https://avatars.githubusercontent.com/u/1652861?v=3"

【讨论】:

  • 这很好,但我只想循环并获取用户名/avatar_url/url
  • @user3079589:同样,Flask 需要返回一个正确的响应。你不能只返回一个列表。你期待的回应是什么? JSON 数据,一个字符串,还是别的什么?请明确。您可能需要更新您的问题以包含该信息。
  • 我期待一个只有用户名、头像和个人资料网址的字符串
  • @KittenCornBall:它们之间有逗号吗?制表符、冒号、换行符?
  • 寻找新行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 2021-10-27
  • 2020-05-25
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多