第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從 api 獲取 json 格式為有用的 json 格式(flask)

從 api 獲取 json 格式為有用的 json 格式(flask)

陪伴而非守候 2023-08-03 17:08:55
我想通過 Flask 將 json 格式的數(shù)據(jù)插入到 web 應(yīng)用程序中,以將值放入 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 轉(zhuǎn)換為 1.format?(我只從 api 獲取 2.json 格式) 或者 在 html 中編寫其他查詢(類似于 p.data.first_name)?
查看完整描述

2 回答

?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您總是以第二種格式檢索輸入數(shù)據(jù),您可以將其轉(zhuǎn)換為第一種格式,如下所示:


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

  }

]


查看完整回答
反對(duì) 回復(fù) 2023-08-03
?
四季花海

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊

您需要的是posts在渲染模板之前過濾并展平第二個(gè) 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))

這兩個(gè)函數(shù)都會(huì)迭代 posts JSON 并提取每個(gè)data節(jié)點(diǎn)中的子節(jié)點(diǎn)。


我認(rèn)為為這個(gè)簡(jiǎn)單的任務(wù)拉一個(gè)庫是不值得的。


查看完整回答
反對(duì) 回復(fù) 2023-08-03
  • 2 回答
  • 0 關(guān)注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)