循环遍历烧瓶json对象 [英] Looping over json object in flask

查看:113
本文介绍了循环遍历烧瓶json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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 /瓶,并不能确定如何做到这一点来完成。最终输出应是如下

This is just spitting out the avatar_url which is great. But now i want to show the username along with a link to the profile. I think this could be done with a loop somehow but I'm still very new to Python/Flask and not really sure how this would be done. The final output should be as follows

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

这应该更新的一切有人进入了搜索表单新值

This should update everything someone enters a new value in the search form

@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标签,并显示出来。

i plan on putting the avatar_url into a html img tag and displaying it.

推荐答案

您可以提取的与列表COM prehension所有的网址:

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

但在瓶你需要返回一个有效的响应。通常,这意味着你返回一个字符串,所以你需要加入这些网址:

but in Flask you do need to return a valid response. Usually that means you return a string, so you'd need to join those URLs:

return '\n'.join(items)

您也可以返回一个JSON列表:

You could also return a JSON list:

from flask import jsonify

# ....

return jsonify(items=items)

这会返回一个JSON对象的键'项目'指着名单; jsonify()功能创建了一个合适的响应()对象以正确的应用程序/ JSON 头为您设置。

This returns a JSON object with the key 'items' pointing to the list; the jsonify() function creates a proper Response() object with the correct application/json header set for you.

在Python 2里,你并不需要取消code JSON响应; GitHub上坚持的JSON RFC并返回UTF-CN codeD数据:

In Python 2, you don't need to decode the JSON response; GitHub sticks to the JSON RFC and returns UTF-encoded data:

@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

如果您想了解更多信息拉成丝状的,我会使用串格式化

If you want to pull out more information into strings, I'd use 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 关键字参数传递的项目不同的密钥。

Each {} placeholder pulls a different key from the item passed in with the i keyword argument.

示范使用相同的搜索:

>>> 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"

这篇关于循环遍历烧瓶json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆