Search code examples
javascriptjsonjsonreader

How to read json


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?


Solution

  • 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.