当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

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>
网友答案:

I modified your function. Check the following code,

  $(document).ready(function() {
  var gettodaystaskdb = "http://enlytica.com/RSLivee/rest/census";
  $.getJSON(gettodaystaskdb, function(todaytask) {
    $(todaytask.Tweets).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);
     console.log(q);
     console.log(r);

    });
});
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: