1

I have a JSON var response (See below) but I can't retreive the value of "reputation" using response.users[0].reputation. I am in Dashcode and it says in error Result of expression response.users[unknown] is not an object. What is the correct syntax?

edit: The variable is dynamically loaded from a XMLHttpRequest. A static var with same json is working.

4 Answers 4

3

I guess from XMLHttpRequest you recieve string but not json object, so you need parse it first in order to get json object, for example using JSON.Parse or jQuery.parseJSON and response.users[0].reputation should work.

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

3 Comments

@umar: I've added references to my answer.
thx - this is working var res = JSON.parse(response); and then res.users[0].reputation
@umar: Cool, but be care before JSON.Parse may not worк in all browsers. I usual copy JSON.Parse code and paste in my own js file. It just a few rows..
3

The JSON is fine. I just checked it and "response.users[0].reputation". (Second pair of eyes and all that.)

I would be more concerned about the "unknown" in "response.users[unknown]". It doesn't look like you are requesting the 0th index of the array "users". Something's going wrong there.

1 Comment

The variable is dynamically loaded from a XMLHttpRequest
1

Personnally when i test the syntax of your Json, it is correct : http://www.jsonlint.com

I have just deleted the comment "//need to get its value".

Comments

1

Your code works for me as you have done it. Maybe you are not assigning the variable correctly (var response = {"total": 1, etc...) or have a typo somewhere

1 Comment

The variable is dynamically loaded from a XMLHttpRequest

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.