2

I'm having problems handling JSON-data as an array in JavaScript. my JSON array looks like this :

    MyArray([['one','two','three'],['four','five','six']]);

In JS I am trying to receive it using this method :

   $.getJSON('http://test.com/array.json', function(data) {
        alert(data);
   }

but i get no data. If i'm using

$get('http://test.com/array.json', function(data) 

instead of $getJSON then i receive my data as a string.

Any help would be greatly appreciated.

2
  • Maybe the respnse isn't valid JSON? Otherwise try using $.getJSON and var parsedJSON = JSON.parse(data); Commented Sep 17, 2012 at 12:22
  • 1
    validate your JSON at http://jsonlint.com/ Commented Sep 17, 2012 at 12:23

3 Answers 3

2

Your data is not JSON, but JSONP, where MyArray is the JSONP callback function.

In theory the web service you're talking to should support a callback= parameter which would be what sets the MyArray name.

If you just add &callback=? then jQuery should automatically generate a randomly named callback function which will then call your success function with the required data.

1

This is no JSON :

 MyArray([['one','two','three'],['four','five','six']]);

A JSON string isn't simply some javascript you want to evaluate but must start by [ or {.

Look at the norm : http://www.json.org/

As you seem to have a constructor at first, this is probably not even JSONP.

I'd recommend you to make a standard JSON array :

"[['one','two','three'],['four','five','six']]"

And then use a constructor client side if you need a MyArray instance.

0

Your JSON isn't valid. The format it's in can still be used. Use the get method and then try the following:

$.get('http://test.com/array.json', function(data) {
  data = data.replace("MyArray(", '"');
  data = data.replace(");", '"');
  var myData = JSON.parse(data); // Converted JSON to object.
}

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.