1

I have following JSON object.

{"feed":[
 {"news":
 {"adopted_from":null,"user_id":null,"description":"this is test","id":2}
 },
 {"news":
 {"adopted_from":null,"user_id":null,"description":"like unlike done","id":1}
 }
]}

I want to retrieve the id of news. I tried in many different ways (e.g. feed[0].news.id, feed.news.id, feed[[0].news.id]) but could not access the value. Can anyone help me how can I access it using JavaScript?

Jonas
131k103 gold badges330 silver badges408 bronze badges
asked Sep 22, 2011 at 9:54

3 Answers 3

2

I copied and pasted your JSON from above and tried the following and it works just fine:


 var data = {"feed":[{"news":{"adopted_from":null,"user_id":null,"description":"this is test","id":2}},{"news":{"adopted_from":null,"user_id":null,"description":"like unlike done","id":1}}]};
 // alert the first news id
 alert(data.feed[0].news.id);

It gets the id from the first news object from the array as intended.

answered Sep 22, 2011 at 10:01
Sign up to request clarification or add additional context in comments.

Comments

1

this works for me:

var f = {"feed":[{"news":{"adopted_from":null,"user_id":null,"description":"this is test","id":2}},{"news":{"adopted_from":null,"user_id":null,"description":"like unlike done","id":1}}]}
alert( f.feed[0].news.id )
answered Sep 22, 2011 at 10:03

Comments

0
var feed = json_decode(yourData)
for(var counter in feed) {
 console.log(feed[counter].news.id);
}
answered Sep 22, 2011 at 10:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.