2

I have a very simple 2D array in a file called synergy_data.json:

[
['2014-08-19',  2956],
['2014-07-19',  2038],
['2014-06-19',  1285],
['2014-05-19',  1383],
['2014-04-19',  1256],
['2014-03-19',  822],
['2014-02-19', 644],
['2014-01-19',  504],
['2013-12-19',  438],
]

I'm trying to call up that data using ajax:

data=[]
$.ajax({
    url: "/marquee/synergy_data.json",
    async: false,
    success: function(resultData) {
        data = resultData;
    }
});
console.log(data)

But the console only spits out "[]"

What am I doing wrong?

***Solution A big thanks to @Quentin for solving this for me. I had two fatal flaws with my JSON: 1) I used single quotes and 2) I had an extra comma. I'll be sure to use JSONLint to check my arrays in the future! (I also removed the "async:false" for good measure.)

1
  • 2
    Don't use async:false, do the logic inside the callback or simply return a jQuery deferred and handle the callback later. Commented Aug 31, 2014 at 8:03

1 Answer 1

3

Your JSON is invalid. The success function won't fire. The error function isn't defined. You end up accessing the array you originally assign to data and not the one from the JSON.

Strings in JSON must be quoted using " characters. ' are not acceptable.

Commas are used to separate items in an array, not terminate them. Remove the trailing comma.

Sign up to request clarification or add additional context in comments.

7 Comments

ouch. should have seen that. +1.
p.s. , would line breaks in json file would be 100% ok ?
Line breaks in JSON are fine so long as they aren't in the middle of strings.
Hey @Quentin, thanks for your answer, but I tried that, and it didn't work. I tried it with apostrophes, with double quotes, without quotes, and by replacing the dates with numbers. Still doesn't work. (But I'll use quotes in the future.)
You had another validation error. You should probably stop writing JSON by hand, or at least use a tool like JSON Lint.
|

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.