gpt4 book ai didi

javascript - 在 Django 中显示来自 API 的 JSON 数据

转载 作者:行者123 更新时间:2023-11-27 23:10:43 24 4
gpt4 key购买 nike

我正在寻找一种简单的方法来显示来自 Django 元素中 API 的 JSON 值。我已经做好了将 JSON 数据读入对象并存储为变量等的所有工作,但我想知道如何以动态方式显示来自 JSON 对象的值。

比如说 JSON 看起来像这样

{
"status":"OK",
"copyright":"Copyright (c) 2017 Pro Publica Inc. All Rights Reserved.",
"results":[
{
"num_results": 10,
"offset": 0,
"bills": [
{
"bill_id": "hr2739-113",
"bill_type": "hr",
"number": "H.R.2739",
"bill_uri": "https://api.propublica.org/congress/v1/113/bills/hr2739.json",
"title": "Efficient Use of Government Spectrum Act of 2013",
"sponsor_title": "Rep.",
"sponsor_id": "M001163",
"sponsor_name": "Doris Matsui",
"sponsor_state": "CA",
"sponsor_party": "D",
"sponsor_uri": "https://api.propublica.org/congress/v1/members/M001163.json",
"gpo_pdf_uri": "http://www.gpo.gov/fdsys/pkg/BILLS-113hr2739ih/pdf/BILLS-113hr2739ih.pdf",
"congressdotgov_url": "https://www.congress.gov/bill/113th-congress/house-bill/2739",
"govtrack_url": "https://www.govtrack.us/congress/bills/113/hr2739",
"introduced_date": "2013-07-18",
"committees": "House Armed Services Committee",
"committee_codes": ["HSAS","HSIF"],
"subcommittee_codes": ["HSAS26","HSIF16"],
"primary_subject": "Science, Technology, Communications",
"summary_short": "Efficient Use of Government Spectrum Act of 2013 - Directs the Federal Communications Commission (FCC), within three years after enactment of the Middle Class Tax Relief and Job Creation Act of 2012, to: (1) reallocate electromagnetic spectrum between the frequencies from 1755 to 1780 megahertz (currently, such frequencies are occupied by the Department of Defense [DOD] and other federal agencies); and (2) as part of the competitive bidding auctions required by such Act, grant new initial lic...",
"latest_major_action_date": "2013-08-29",
"latest_major_action": "Referred to the Subcommittee on Intelligence, Emerging Threats & Capabilities."
},
{
"bill_id": "hr3355-113",
"bill_type": "hr",
"number": "H.R.3355",
"bill_uri": "https://api.propublica.org/congress/v1/113/bills/hr3355.json",
"title": "Reducing Employer Burdens, Unleashing Innovation, and Labor Development Act of 2013",
"sponsor_title": "Rep.",
"sponsor_id": "G000558",
"sponsor_name": "Brett Guthrie",
"sponsor_state": "KY",
"sponsor_party": "R",
"sponsor_uri": "https://api.propublica.org/congress/v1/members/G000558.json",
"gpo_pdf_uri": "http://www.gpo.gov/fdsys/pkg/BILLS-113hr3355ih/pdf/BILLS-113hr3355ih.pdf",
"congressdotgov_url": "https://www.congress.gov/bill/113th-congress/house-bill/3355",
"govtrack_url": "https://www.govtrack.us/congress/bills/113/hr3355",
"introduced_date": "2013-10-28",
"committees": "House Armed Services Committee",
"primary_subject": "Economics and Public Finance",
"summary_short": "Reducing Employer Burdens, Unleashing Innovation, and Labor Development Act of 2013 - Expresses the sense of Congress that increasing the competitiveness of U.S. manufacturers will strengthen the national economy. Title I: Investing in America's Workforce - Investing in America's Workforce Act - Amends the Workforce Investment Act of 1998 to require state or local workforce investment systems to use youth activities funds allocated to a local area for programs that provide training, which may...",
"latest_major_action_date": "2014-01-24",
"latest_major_action": "Referred to the Subcommittee on Intelligence, Emerging Threats & Capabilities."
},

我希望它以 HTML 2 不同的条目返回。但是,例如,如果此 API 调用要返回 5 个条目,我希望它以 HTML 格式返回 5 个不同的条目。处理此问题的最佳方法是什么?

最佳答案

您可以在 JSON 对象上循环,例如,创建表格行来显示结果。就像您的 JSON 对象名称是“结果”一样。您可以使用如下所示的 jquery 遍历“账单”。我刚刚添加了前 4 个元素。您可以根据您的 Json 对象添加尽可能多的内容。最后,“trHTML”可以作为表格添加到 HTML 页面。循环会动态迭代任意数量的账单,例如 4、5、100

$.each(data['bills'] , function (index, item) {
trHTML += '<tbody><tr><td>'+item[0]+'</td><td>'+item[1]+'</td><td>'+item[2]+
'</td><td>'+item[3]+'</td></tr></tbody>';

});

关于javascript - 在 Django 中显示来自 API 的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58533690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com