【笔记】JSON文件提取为JS对象

前言

JSON文件提取为JS对象

JSON文件

  • 创建一个.json文件存放数据
filename.json
1
2
3
4
5
6
7
8
9
10
11
12
{
"father": [
{
"key1": "value",
"key2": "value"
},
{
"key1": "value",
"key2": "value"
}
]
}

通过fetch函数提取为JS对象

1
2
3
4
5
6
7
8
fetch(json_path)
.then(response => response.json())
.then(result => {
for (let {key1, key2} of result.father) {
console.log(key1);
console.log(key2);
}
});

通过jQuery提取为JS对象

  • 通过jQuery封装的AJAX提取JSON文件为JS对象
1
2
3
4
5
6
$.ajax({
url: "filename.json",
success: function (data) {
console.log(data);
}
});

完成

参考文献

CSDN——q394503873