显示json,json的定义

作为老师,我可以为您提供一些示例代码,以演示如何使用<li><table>和复选框(checkbox)来获取外部JSON数据。

  1. 使用<li>列表显示外部JSON数据:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用<li>显示外部JSON数据</title>
    <script>
        // 创建 XMLHttpRequest 对象
        let request = new XMLHttpRequest();
        // 发起 GET 请求,获取 JSON 数据
        request.open("GET", "data.json", true);
        // 监听请求状态变化
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                // 将 JSON 数据解析为 JavaScript 对象
                let data = JSON.parse(request.responseText);
                // 获取 <ul> 元素
                let ul = document.getElementById("jsonList");
                // 遍历数据填充列表项
                data.forEach(function(item) {
                    let li = document.createElement('li');
                    li.innerText = item.name;
                    ul.appendChild(li);
                });
            }
        }
        // 发送请求
        request.send();
    </script>
</head>
<body>
    <ul id="jsonList"></ul>
</body>
</html>
  1. 使用<table>显示外部JSON数据:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用<table>显示外部JSON数据</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: left;
        }
    </style>
    <script>
        // 创建 XMLHttpRequest 对象
        let request = new XMLHttpRequest();
        // 发起 GET 请求,获取 JSON 数据
        request.open("GET", "data.json", true);
        // 监听请求状态变化
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                // 将 JSON 数据解析为 JavaScript 对象
                let data = JSON.parse(request.responseText);
                // 获取 <table> 元素
                let table = document.getElementById("jsonTable");
                // 创建表头
                let thead = table.createTHead();
                let headerRow = thead.insertRow();
                Object.keys(data[0]).forEach(function(key) {
                    let th = document.createElement("th");
                    th.innerText = key;
                    headerRow.appendChild(th);
                });
                // 填充表格数据
                let tbody = table.createTBody();
                data.forEach(function(item) {
                    let row = tbody.insertRow();
                    Object.values(item).forEach(function(value) {
                        let cell = row.insertCell();
                        cell.innerText = value;
                    });
                });
            }
        }
        // 发送请求
        request.send();
    </script>
</head>
<body>
    <table id="jsonTable"></table>
</body>
</html>
  1. 使用复选框显示外部JSON数据:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用复选框显示外部JSON数据</title>
    <script>
        // 创建 XMLHttpRequest 对象
        let request = new XMLHttpRequest();
        // 发起 GET 请求,获取 JSON 数据
        request.open("GET", "

data.json", true);
        // 监听请求状态变化
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                // 将 JSON 数据解析为 JavaScript 对象
                let data = JSON.parse(request.responseText);
                // 获取容器元素
                let container = document.getElementById("checkboxContainer");
                // 遍历数据创建复选框
                data.forEach(function(item) {
                    let label = document.createElement("label");
                    let checkbox = document.createElement("input");
                    checkbox.type = "checkbox";
                    checkbox.value = item.name;
                    label.appendChild(checkbox);
                    label.appendChild(document.createTextNode(item.name));
                    container.appendChild(label);
                });
            }
        }
        // 发送请求
        request.send();
    </script>
</head>
<body>
    <div id="checkboxContainer"></div>
</body>
</html>

以上是三个示例代码,分别展示了如何使用<li><table>和复选框来获取外部JSON数据,并根据数据填充相关元素。请确保将data.json替换为实际的JSON数据文件的路径。
当然,我可以为您提供对应的JSON数据示例。以下是与前面示例代码匹配的JSON数据:

  1. data.json(用于使用<li>显示外部JSON数据):
[
  { "name": "Item 1" },
  { "name": "Item 2" },
  { "name": "Item 3" },
  { "name": "Item 4" }
]
  1. data.json(用于使用<table>显示外部JSON数据):
[
  { "name": "John", "age": 25, "country": "USA" },
  { "name": "Emma", "age": 28, "country": "UK" },
  { "name": "Satoshi", "age": 31, "country": "Japan" },
  { "name": "Sophie", "age": 22, "country": "France" }
]
  1. data.json(用于使用复选框显示外部JSON数据):
[
  { "name": "Option 1" },
  { "name": "Option 2" },
  { "name": "Option 3" },
  { "name": "Option 4" }
]

请将相应的JSON数据保存为名为data.json的文件,并将其与相应的HTML文件放置在同一目录下,以使示例代码能够正确加载和解析JSON数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值