0

If I have json data which looks like this:

{"d":1}

How do I check if that 1 is a 1 or a 0?

I have tried the following, but it goes straight to the else, even when I can see that the json data has a 1.

success: function(data) {

    if (data[0]) {
        console.log("Results...");
    } else {
        console.log("No results...");
    }

the data contains {"d":1}

1
  • You forgot javascript in the tags Commented Aug 2, 2013 at 11:25

6 Answers 6

4
  if (data["d"] == 1)

or simply

  if (data.d == 1)
Sign up to request clarification or add additional context in comments.

Comments

1

data is a hash, so this should work:

if (data.d == 1) {...}

Comments

0

You'd be right if the JSON was actually an array:

[{"d":1}]

Since it's not, you can simply

if (data) {
    // Do stuff
}

Comments

0

You must use the "d" key to access the data associated with it, which is 1.

JSON is simply key-value pairs.

e.g.

if (data.d == 1) {
...
}

Comments

0

You can do:

data.d == 1

Example: http://jsfiddle.net/avVRs/

Comments

0

Try this

if (data.d == 1){
   // do
}

Comments

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.