为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

陪伴而非守候 2023-08-03 17:08:55
我想通过 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)?
查看完整描述

2 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

如果您总是以第二种格式检索输入数据,您可以将其转换为第一种格式,如下所示:


import itertools

flatten = itertools.chain.from_iterable


def transform(posts):

    transformed = list(map(lambda post: post["data"], posts))

    flat_posts = list(flatten(transformed))

    return flat_posts

例子:


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":None,"per_page":25,"total_count":1}

    }

]


print(transform(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

  }

]


查看完整回答
反对 回复 2023-08-03
?
四季花海

TA贡献1811条经验 获得超5个赞

您需要的是posts在渲染模板之前过滤并展平第二个 JSON。例如,你可以这样做;


def fatten(json):

    flatten_json = []

    for node in json:

        d = node["data"]

        if d is not None:

            for item in d:

                flatten_json.append(item)

    return flatten_json


或者更多Pythonic(但不那么可读)的版本


def flatten(json):

    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节点中的子节点。


我认为为这个简单的任务拉一个库是不值得的。


查看完整回答
反对 回复 2023-08-03
  • 2 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信