js 简单读取json格式的变量的简单例子

Posted 霜序0.2℃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 简单读取json格式的变量的简单例子相关的知识,希望对你有一定的参考价值。

json

let users = [
	{
		"name": "Bill",
		"sex": "1",
		"phone": "1377777777"
	},
	{
		"name": "Biaall",
		"sex": "1",
		"phone": "13464537564"
	},
	{
		"name": "Billgdsaf",
		"sex": "2",
		"phone": "13772345235"
	}
];

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<table>
    <caption>table</caption>
    <tr>
        <th>name</th>
        <th>sex</th>
        <th>phone</th>
    </tr>
    <tr>
        <td><div class="name"></div></td>
        <td><div class="sex"></div></td>
        <td><div class="phone"></div></td>
    </tr>
    <tr>
        <td><div class="name"></div></td>
        <td><div class="sex"></div></td>
        <td><div class="phone"></div></td>
    </tr>
    <tr>
        <td><div class="name"></div></td>
        <td><div class="sex"></div></td>
        <td><div class="phone"></div></td>
    </tr>
</table>
<script>
	let users = [
		{
			"name": "Bill",
			"sex": "1",
			"phone": "1377777777"
		},
		{
			"name": "Biaall",
			"sex": "1",
			"phone": "13464537564"
		},
		{
			"name": "Billgdsaf",
			"sex": "2",
			"phone": "13772345235"
		}
	];

	let names = document.getElementsByClassName("name");
	let sexs = document.getElementsByClassName("sex");
	let phones = document.getElementsByClassName("phone");
	for (let i = 0; i < 3; i++) {
		names[i].innerText = users[i].name;
		sexs[i].innerText = users[i].sex;
		phones[i].innerText = users[i].phone;
	}
</script>
</body>
</html>

以上是关于js 简单读取json格式的变量的简单例子的主要内容,如果未能解决你的问题,请参考以下文章

GraphQL 片段 JSON 格式

怎么用jsp生成json格式数据,然后用js又怎样读取其数据?

JAVA 如何读取JSON编码格式的数据?

JS和PHP之间以JSON格式传输

JSON 简单例子

如何将请求参数转为json格式