views:

544

answers:

5

I can't seem to access my objects.

after parsing the server string:

var json = JSON.parse(myJsonText):

I get the below with an alert:

alert(json.param1)

{"ID":17,"Name":"swimming pools","ParentID":4,"Path":""},
{"ID":64,"Name":"driveways","ParentID":4,"Path":""}

Now, I am trying to access ID and Name.

I have tried:

json.param1[0].ID
json.param1[0]["ID"]
json.param1[0][0]

And a lot of others that really don't make much sense such as:

json[0].ID or 
json.param1.ID etc...

I am getting (for example, in the case of json.param1[0].ID):

param1.0.ID is null or not an object.

Any ideas?

+2  A: 

That looks like invalid JSON. Try wrapping it in the brackets which makes it a valid array of JSON objects. Then access it by index.

[
    {"ID":17,"Name":"swimming pools","ParentID":4,"Path":""},
    {"ID":64,"Name":"driveways","ParentID":4,"Path":""}    
]
ChaosPandion
he said that that is the output from calling `alert(json.param1)`
nickf
Regardless that is still invalid JSON.
ChaosPandion
well that's my point: it's not JSON, it's the output from an object which was apparently created successfully from some JSON.
nickf
@nickf: I would have expected to see [object] or something else in an alert if it were a valid object being alert. I don't believe an object's JSON format is alerted.
David Andres
It's an array, not an object. if you `alert()` an array, it will alert `array.join(",")`. try it out: `alert([1, 2]) ==> "1,2"`
nickf
@nickf: Look beyond the array to the objects within. ;)`alert(json.param1)` should be `"[object],[object]"`
Jonathan Lonowski
alerting json gives me object, object.
Code Sherpa
+2  A: 

If you are receiving the raw JSON in the alert, then that would lead me to believe there is a problem w/ the JSON you are trying to parse.

Kyril
if there were a problem with the raw JSON, then he wouldn't be able to see the value of `json.param1`...
nickf
The array is probably fine, but it seems the objects within are being parsed as strings: (abbr.) `["{\"ID\":17}","{\"ID\":64}"]` Thus, seeing `{"ID":17},{"ID":64}` when alerted.
Jonathan Lonowski
A: 

if json.param1 is what you said it is, then json.param1[0].ID should work (and evaluate to "17").

If it's not working, could you show us the text you're parsing to generate the JSON object?

nickf
+1  A: 

To compile and expand on all of the comments... ;)


Your first clue that something's wrong is your alert:

alert(json.param1)

Instead of getting:

{"ID":17,"Name":"swimming pools","ParentID":4,"Path":""},
{"ID":64,"Name":"driveways","ParentID":4,"Path":""}

You should be getting something similar to the following:

[object],[object]


Try alerting the typeof array element, itself:

alert(typeof json.param1[0]) //=> should say "object"

If you get anything besides "object", either the JSON isn't formatted correctly or the parser is failing.


One good clue as to which is wrong is if the original JSON looks like this:

{"param1" : [
  "{\"ID\":17,\"Name\":\"swimming pools\",\"ParentID\":4,\"Path\":\"\"}",
  "{\"ID\":64,\"Name\":\"driveways\",\"ParentID\":4,\"Path\":\"\"}"
]}

Then, it's probably the JSON that's broken. (Sorry ;)

On the other hand, if your JSON looks like this:

{"param1" : [
  {"ID":17,"Name":"swimming pools","ParentID":4,"Path":""},
  {"ID":64,"Name":"driveways","ParentID":4,"Path":""}
]}

Then, it's probably the parser.

Jonathan Lonowski
+2  A: 

Try this

// you already have this bit
var json = JSON.parse(myJsonText);
alert(json.param1);

// add this
var tmp_param1 = JSON.parse(json.param1);
json.param1 = tmp_param1;
alert(json.param1);  // should print [object, object] or similar

alert(json.param1[0].ID);  // should print "17"
alert(json.param1[0].Name);  // should print "swimming pools"
system PAUSE
Thanks, that did the trick. What is going wrong on the server end that I have to do this in the first place?
Code Sherpa
The server is sending the main JSON text (`myJsonText`) as an array of quoted *strings*, rather than an array of JSON *objects*. Each string is a JSON text that requires the second `parse` to get at the objects. If you `alert(myJsonText)` and look carefully, I bet you'll see the extra quotation marks.
system PAUSE
Yeah, that is what is happening. I am guessing there is no real reason to revisit my server logic and change how the JSON is being delivered to the client. I'll use your solution for future parsing. Thanks again.
Code Sherpa