Ali F
Ali F

Reputation: 78

Cannot parse JSON array

I have this json and trying to parse it using 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"]
    ]
}

After parsing, the console shows this just as it is printed from php file. However, it can not find values when I'm trying to print a specified value in the console and gives me this error:

Uncaught TypeError: Cannot read property '0' of undefined

For example when printing first value of the array inside data1 object ("1 06, 2016 23:27:11") using:

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

Upvotes: 2

Views: 880

Answers (1)

Manuel Garcia
Manuel Garcia

Reputation: 144

Try

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

Upvotes: 1

Related Questions