JSON(JavaScript 物件表示法)是一種強大的資料格式,用於在伺服器和用戶端之間交換資料。 HTML 表格是以表格格式表示資料的強大工具,使其變得非常易於閱讀、分析和比較。在 Web 開發中,將 JSON 資料轉換為 HTML 表格是很常見的。
在本文中,我們將學習如何使用 Javascript 和 jQuery 將 JSON 資料轉換為 HTML 表格。讀完本文後,您將對 JSON 到 HTML 表的轉換有深入的了解。
使用 JavaScript 將 JSON 資料轉換為 HTML 表格
以下是使用 JSON 資料建立 HTML 表格的步驟。
建立一個名為「convert」的函數。
建立範例 JSON 資料。
使用 getElementByID(“container”) 取得容器,我們將在其中追加表格。
取得 JSON 資料的第一個物件的鍵,以便我們取得表格的標題。
循環列名稱,建立標題單元格,並將列名稱設定為標題單元格的文字。
將標題單元格附加到標題行,然後將標題行附加到標題
將標題附加到表格
循環 JSON 數據,建立表格行,使用 Object.values(item) 取得 JSON 資料中目前物件的值,並建立表格儲存格。
將值設為表格單元格的文本,將表格單元格附加到表格行,然後將表格行附加到表格。
範例
在此範例中,我們使用 Javascript 將 JSON 資料轉換為 HTML 表。
<html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } td, th { padding: 10px; } </style> </head> <body> <h2 id="Convert-JSON-data-into-a-html-table-using-Javascript">Convert JSON data into a html table using Javascript</h2> <p>Click the following button to convert JSON results into HTML table</p><br> <button id="btn" onclick="convert( )"> Click Here </button> <br> <h3 id="Resulting-Table"> Resulting Table: </h3> <div id="container"></div> <script> // Function to convert JSON data to HTML table function convert() { // Sample JSON data let jsonData = [ { name: "Saurabh", age: "20", city: "Prayagraj" }, { name: "Vipin", age: 23, city: "Lucknow", }, { name: "Saksham", age: 21, city: "Noida" } ]; // Get the container element where the table will be inserted let container = document.getElementById("container"); // Create the table element let table = document.createElement("table"); // Get the keys (column names) of the first object in the JSON data let cols = Object.keys(jsonData[0]); // Create the header element let thead = document.createElement("thead"); let tr = document.createElement("tr"); // Loop through the column names and create header cells cols.forEach((item) => { let th = document.createElement("th"); th.innerText = item; // Set the column name as the text of the header cell tr.appendChild(th); // Append the header cell to the header row }); thead.appendChild(tr); // Append the header row to the header table.append(tr) // Append the header to the table // Loop through the JSON data and create table rows jsonData.forEach((item) => { let tr = document.createElement("tr"); // Get the values of the current object in the JSON data let vals = Object.values(item); // Loop through the values and create table cells vals.forEach((elem) => { let td = document.createElement("td"); td.innerText = elem; // Set the value as the text of the table cell tr.appendChild(td); // Append the table cell to the table row }); table.appendChild(tr); // Append the table row to the table }); container.appendChild(table) // Append the table to the container element } </script> </body> </html>
範例:使用 jQuery 將 JSON 資料轉換為 HTML 表格
這裡是使用 jQuery 將 JSON 資料轉換為 HTML 表的程式碼。
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } td, th {padding: 10px;} </style> </head> <body> <h2 id="Convert-JSON-data-into-a-html-table-using-Jquery">Convert JSON data into a html table using Jquery</h2> <p>Click the following button to convert JSON results into HTML table</p> <br> <button id="btn" onclick="convert( )"> Click Here </button> <br> <h3 id="Resulting-Table"> Resulting Table: </h3> <div id="container"></div> <script> // Function to convert JSON data to HTML table function convert() { // Sample JSON data let jsonData = [ { name: "Saurabh", age: "20", city: "Prayagraj" }, { name: "Vipin", age: 23, city: "Lucknow", }, { name: "Saksham", age: 21, city: "Noida" } ]; // Get the container element where the table will be inserted let container = $("#container"); // Create the table element let table = $("<table>"); // Get the keys (column names) of the first object in the JSON data let cols = Object.keys(jsonData[0]); // Create the header element let thead = $("<thead>"); let tr = $("<tr>"); // Loop through the column names and create header cells $.each(cols, function(i, item){ let th = $("<th>"); th.text(item); // Set the column name as the text of the header cell tr.append(th); // Append the header cell to the header row }); thead.append(tr); // Append the header row to the header table.append(tr) // Append the header to the table // Loop through the JSON data and create table rows $.each(jsonData, function(i, item){ let tr = $("<tr>"); // Get the values of the current object in the JSON data let vals = Object.values(item); // Loop through the values and create table cells $.each(vals, (i, elem) => { let td = $("<td>"); td.text(elem); // Set the value as the text of the table cell tr.append(td); // Append the table cell to the table row }); table.append(tr); // Append the table row to the table }); container.append(table) // Append the table to the container element } </script> </body> </html>
以上是如何使用 JavaScript/jQuery 將 JSON 資料轉換為 html 表?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1
強大的PHP整合開發環境