亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 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 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號