gpt4 book ai didi

Javascript 解析嵌套的 JSON 对象

转载 作者:行者123 更新时间:2023-11-30 12:28:08 25 4
gpt4 key购买 nike

我是 javascript 的新手,我正在尝试解析和操作下面的数据,但不太确定如何去做。有人可以帮忙吗?

JSON 数据

{
"2009-01": {
"bbcfour": 324,
"bbcnews24": 1075,
"bbcone": 940,
"bbcthree": 441,
"bbctwo": 1040,
"cbbc": 898,
"cbeebies": 1343
},
"2009-02": {
"bbcfour": 295,
"bbcnews24": 958,
"bbcone": 904,
"bbcthree": 434,
"bbctwo": 1038,
"cbbc": 793,
"cbeebies": 1246
}}

JavaScript

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-language" content="en" />
<meta http-equiv="Content-Type" content="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<link href="main.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="jquery-2.1.1.js"></script>
<script>
$.getJSON('data.json', function (data) {
console.log(data);
});
</script>
</body>
</html>

最佳答案

使用for...in 循环。

for..in 语句以任意顺序迭代对象的可枚举属性。

   $.getJSON('data.json', function (data) {
var resp = data;
for(k in resp)
{
console.log(resp[k].bbcfour);
console.log(resp[k].bbcnews24);
console.log(resp[k].bbcone);
// And so on
}
});

关于Javascript 解析嵌套的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28649023/

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