猿问

从 api 获取 json 格式为有用的 json 格式(flask)

我想通过 Flask 将 json 格式的数据插入到 web 应用程序中,以将值放入 html 中:


spec_player.html:


{% for p in posts: %}

    <p>{{p.first_name}}</p>

{% endfor %}

这有效(main.py):

posts = [

    {

        "id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",

        "team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250

    }

]


@app.route("/spec")

def spec_player():

    return render_template("spec_player.html", posts=posts)

这不起作用(main.py):

posts = [

    {

        "data":[{"id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",

        "team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250}],

        "meta":{"total_pages":1,"current_page":1,"next_page":null,"per_page":25,"total_count":1}

    }

]


@app.route("/spec")

def spec_player():

    return render_template("spec_player.html", posts=posts)

我想知道是否有办法将 2.json-format 转换为 1.format?(我只从 api 获取 2.json 格式) 或者 在 html 中编写其他查询(类似于 p.data.first_name)?


陪伴而非守候
浏览 122回答 2
2回答

胡说叔叔

如果您总是以第二种格式检索输入数据,您可以将其转换为第一种格式,如下所示:import itertoolsflatten = itertools.chain.from_iterabledef transform(posts):&nbsp; &nbsp; transformed = list(map(lambda post: post["data"], posts))&nbsp; &nbsp; flat_posts = list(flatten(transformed))&nbsp; &nbsp; return flat_posts例子:posts = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "data":[{"id":237,"first_name":"LeBron","height_feet":6,"height_inches":8,"last_name":"James","position":"F",&nbsp; &nbsp; &nbsp; &nbsp; "team":{"id":14,"abbreviation":"LAL","city":"Los Angeles","conference":"West","division":"Pacific","full_name":"Los Angeles Lakers","name":"Lakers"},"weight_pounds":250}],&nbsp; &nbsp; &nbsp; &nbsp; "meta":{"total_pages":1,"current_page":1,"next_page":None,"per_page":25,"total_count":1}&nbsp; &nbsp; }]print(transform(posts))>>> [&nbsp; {&nbsp; &nbsp; 'id': 237, 'first_name': 'LeBron', 'height_feet': 6, 'height_inches': 8, 'last_name': 'James', 'position': 'F',&nbsp;&nbsp; &nbsp; 'team': {'id': 14, 'abbreviation': 'LAL', 'city': 'Los Angeles', 'conference': 'West', 'division': 'Pacific', 'full_name': 'Los Angeles Lakers', 'name': 'Lakers'}, 'weight_pounds': 250&nbsp; }]

四季花海

您需要的是posts在渲染模板之前过滤并展平第二个 JSON。例如,你可以这样做;def fatten(json):&nbsp; &nbsp; flatten_json = []&nbsp; &nbsp; for node in json:&nbsp; &nbsp; &nbsp; &nbsp; d = node["data"]&nbsp; &nbsp; &nbsp; &nbsp; if d is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for item in d:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flatten_json.append(item)&nbsp; &nbsp; return flatten_json或者更多Pythonic(但不那么可读)的版本def flatten(json):&nbsp; &nbsp; return [item for node in json if node["data"] is not None for item in node["data"]]然后将扁平化的 json 传递为return render_template("spec_player.html", posts=fatten(posts))这两个函数都会迭代 posts JSON 并提取每个data节点中的子节点。我认为为这个简单的任务拉一个库是不值得的。
随时随地看视频慕课网APP

相关分类

Python
我要回答