JSON with objects named "-1"

I need to take JSON from the site and parse it. The problem is that objects are called "-1", "-2", etc.

When I try to parse it, Firebug returns with: "TypeError: obj is undefined length = obj.length"

Chrome returns with a similar message: "Not prepared TypeError: cannot read property" length "undefined"

For reference, here is the JSON sniper:

{ "-1": { "number": 47, "properties": [ [ 

And here is the code I'm trying to use.

 $.getJSON("http://www.website.com/builds?select=-1&select=-10",function(data){ $.each(data.-1, function(info,value){ 

If I download JSON, save it locally and rename โ€œ-1โ€ to โ€œoneโ€, rename โ€œ-2โ€ to โ€œtwoโ€, then it works fine. EG. JSON becomes:

 { "one": { "number": 47, "properties": [ [ 

And my code is getting

 $.getJSON("C:\json.json",function(data){ $.each(data.one, function(info,value){ 

This works great, but adds another tricky step.

Is there a way to parse the original JSON (since it is generated automatically from the server and changes often), or am I stuck trying to figure out how to save it locally and change the names of the objects before parsing?

+4
source share
1 answer

data.-1 must be specified as a string using parentheses.

data["-1"]

+7
source

All Articles