1

How will I be able to get the value of updated in this json that is from my web service:

{"listener":{"id":"1","updated":"false"}}

Here's what I've tried:

$.ajax({
       ...
        success : function(data) {            
            console.log("received listener: " + data);            
            var received = JSON.parse(JSON.stringify(data));                              
            var listener = received.listener;  
            var length = listener.length;
            //alert('returned json' + data + ' no. of products received: ' + length);
            console.log('returned json' + listener + ' no. of listener received: ' + length); //it says undefined for the length

            var updated = listener[0].updated;

        }
    });

Thanks.

4
  • JSON.parse(JSON.stringify(data)); :o Commented Jul 23, 2012 at 3:48
  • Also, an Object does not have a length property. What is your bug, exactly? Commented Jul 23, 2012 at 3:49
  • I want to get the value of updated which is false. Commented Jul 23, 2012 at 5:11
  • I got it, like Hamish said. listener.updated Commented Jul 23, 2012 at 5:13

1 Answer 1

2

Firstly, this line doesn't make sense:

var received = JSON.parse(JSON.stringify(data));    

JSON.stringify converts a JSON structure to a string, and JSON.parse does the opposite. In other words:

var received = data.listener; // is equivalent.

Secondly, received is an object:

{"id":"1","updated":"false"}

It's not an array, so it does not have a length property. If you were looking to get the id of the received object then you'd obviously use:

var updated = listener.id;

For your code to work data would have to look like this:

{"listener":[{"id":"1","updated":"false"}]}
Sign up to request clarification or add additional context in comments.

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.