Search code examples
jqueryjsoneachgetjson

reading data from Json file format and using it without localstorage


I have been trying to read data from a Json source and i am getting the data in the browsers console as follows :

 Object {Tweets: Array[10]}
 Tweets: Array[10]
 0: Object
 1: Object
 2: Object
 3: Object
 4: Object
 5: Object
 6: Object
 7: Object
 8: Object
 9: Object

in Addition each Object has more data:

  0: {_id: {$oid: "554d57f7e97e6699f5244d9d"}, TWITTER_ID: 2911695560, USER_ID: "55406d3ee97e79138156d553",…}
  1: {_id: {$oid: "554d57f7e97e6699f5244d9e"}, TWITTER_ID: 2911695560, USER_ID: "55406d3ee97e79138156d553",…}

and i am trying to access them through my Jquery code .Is there a way to loop through them all.Here is how i am trying to access each objects data

 $(document).ready(function () {
     var gettodaystaskdb = "http://enla.com/Livee/rt/sus";

     $.getJSON(gettodaystaskdb, function (todaytask) {

         $(todaytask).each(function (index, element) {
             console.log(todaytask);
             console.log(index);
             console.log(element);

             var p = element.TWEET_ID;
             var q = element.TWEET;
             var r = element.TWEET_TIME;

             console.log(p);
         });

     });
 });

element.key doesn't seem to work and all i get is "Undefined" .Is there a way that could possibly help me show the whole raw data .


Solution

  • You looped the wrong part. This maybe a correction:

    $(document).ready(function () {
      var gettodaystaskdb = "http://enlytica.com/RSLivee/rest/census";
    
      $.getJSON(gettodaystaskdb, function (todaytask) {
        console.log(todaytask);
    
        $.each(todaytask["Tweets"], function (index, element) {
          console.log(index);
          console.log(element);
    
          var p = element.TWEET_ID;
          var q = element.TWEET;
          var r = element.TWEET_TIME;
    
          console.log(p);
        });
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>