无法解析JSON数组

时间:2016-01-06 20:14:40

标签: javascript jquery json ajax

我有这个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]);

1 个答案:

答案 0 :(得分:1)

尝试

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