0

Sorry for basic question, but this doesn't seem like properly formed JSON to me. I am reading this structure in from a file that returns this:

states([{"code":"AL","name":"Alabama"},{"code":"AK","name":"Alaska"},{"code":"AZ","name":"Arizona"}])

When I did a JSON Lint on this, it fails - but its what I have to work with, as this is a file on a server separate from mine.

I expected it to be var stores = [{stuff}], I have yet to see the JSON saved like above, but again - I'm noob

I'm using Angular to read this in, so here's what I have in my code:

var directory = this;
directory.states = [];
$http.get('http://somesite.com/js/states.json').success(function(data){
    directory.states = data;
});

Here is the file I am having problems with: http://massachusettswebdesigns.com/states.json

If I treat the states in the file as a variable, I get an undefined error:

var state = data.states;

Its not really a function either.

Any Ideas? I guess I could treat the whole thing as a string and split off the states( part, but I am thinking that there is an easier solution that I am just missing.

Waterskier19
  • 136
  • 1
  • 11
  • 5
    That is intended to be sourced into your page by a ` – Pointy Apr 02 '15 at 18:18

2 Answers2

0

That is definitely not correctly formatted JSON. The formatting that you are showing is a function call (states( . . . )) that is taking a "JSON-like" array ([ . . . ]) of objects ({"code":"XX","name":"yyyyy"}) (per Pointy's comment, JSONP).

If it is being returned to you in a string format, you can use some string maniputation (replace or som kind of RegExp match) to trim out the function part of what is being returned and then parse the rest, and then you should be able to treat it like a JSON feed at that point.

EDIT: Pointy's comment is correct about it being JSONP. If you are prepared to handle JSONP, then, that would be the proper way to work with it. If not, then you can follow my suggestion of trimming out the "JSONP parts" and be able to use it, as if it were a JSON feed.

Here is a good SO thread on the differences between JSON and JSONP, for reference: What are the differences between JSON and JSONP?

Community
  • 1
  • 1
talemyn
  • 7,822
  • 4
  • 31
  • 52
  • So this is JSONP, I am still not 100% sure what to do now - I can grab it in my data object - but then what do I need to do? – Waterskier19 Apr 02 '15 at 19:07
-2

Using the open source project jinqJs The JSON looks valid to me taking out the outside status(). I agree it looks like a function call.

you would simply do:

var result = jinqJs().from('http://someurl').select();

OR

Async call

jinqJs().from('http://someurl', function(self) {result = self.select();});
NYTom
  • 524
  • 2
  • 14