gpt4 book ai didi

javascript - 无法解析 JSON 数组

转载 作者:行者123 更新时间:2023-11-30 00:16:07 24 4
gpt4 key购买 nike

我有这个 json 并尝试使用 Ajax 解析它。

{
"data1": ["1 06, 2016 23:27:11", "22.4", "26.3", "866.81"],
"data2": [
["1 06, 2016 21:55:39", "1 06, 2016 22:05:49", "1 06, 2016 22:15:59", "1 06, 2016 22:26:10", "1 06, 2016 22:36:19", "1 06, 2016 22:46:30", "1 06, 2016 22:56:41", "1 06, 2016 23:06:51", "1 06, 2016 23:17:01", "1 06, 2016 23:27:11"],
["22.1", "22.2", "22.5", "22.6", "22.7", "21.5", "22.6", "22.6", "22.5", "22.4"],
["25.7", "26.8", "27.1", "27.2", "27.2", "26.3", "26.9", "26.7", "26.4", "26.3"],
["866.9", "866.64", "866.81", "866.61", "866.53", "866.65", "866.51", "866.65", "866.67", "866.81"]
],
"data3": [
["1 03, 2016", "1 04, 2016", "1 05, 2016", "1 06, 2016"],
["21.8", "21.1", "20.5", "20.3"],
["23.8", "24.1", "24.2", "23.4"]
],
"data4": [
["1 03, 2016", "1 04, 2016", "1 05, 2016", "1 06, 2016"],
["32.7", "28", "22.9", "23.5"],
["35.7", "32.8", "29.5", "28.5"]
],
"data5": [
["1 03, 2016", "1 04, 2016", "1 05, 2016", "1 06, 2016"],
["869.31", "870.46", "867.2", "864.37"],
["872.57", "875.91", "875.54", "869.3"]
]
}

解析后,控制台显示它就像从 php 文件中打印出来的一样。但是,当我尝试在控制台中打印指定值时它找不到值,并出现此错误:

Uncaught TypeError: Cannot read property '0' of undefined

例如,当打印 data1 对象 ("1 06, 2016 23:27:11") 中数组的第一个值时,使用:

var jsonData = $.ajax({
url: "source.php",
dataType:"json",
async: false
}).responseText;
console.log(jsonData);
console.log(jsonData.data1[0]);

最佳答案

尝试

$.getJSON( "source.php", function( json ) {
console.log(json.data1[0]);
});

关于javascript - 无法解析 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34641922/

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