2

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]);
8
  • 1
    What shows on console.log(jsonData.data1); ? Commented Jan 6, 2016 at 20:22
  • it returns: undefined Commented Jan 6, 2016 at 20:26
  • You are using .responceText and dataType: "json" are they compatible? After all jquery shoudl have converted it to a json object. Or maybe not in this case so you may have to manually convert the string to a json object if you are catching it as text Commented Jan 6, 2016 at 20:29
  • I think you are printing the response when the ajax petition hasn't been solved yet. Commented Jan 6, 2016 at 20:30
  • your last line should be console.log(JSON.parse(jsonData).data1[0]);, since jsonData is still a string at that point... Commented Jan 6, 2016 at 20:31

1 Answer 1

1

Try

$.getJSON( "source.php", function( json ) {
  console.log(json.data1[0]);
});
Sign up to request clarification or add additional context in comments.

2 Comments

This returns the desired value properly.
If you want to use a ajax petition I recoomend : api.jquery.com/jquery.ajax but if you want make a ajax petition which return a json I reccomend: api.jquery.com/jquery.getjson

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.