0

How to mapping this json ?, i have been trying with multiple argument on each, but still can't get the data.

{ 
  "data" : { 
      "after" : "t3_qp79c",
      "before" : null,
      "children" : [ { 
            "data" : { "url" : "http://imgur.com/c0COJ" },
            "kind" : "t3"
      } ],
      "modhash" : ""
  },
  "kind" : "Listing"
}

My javascript code

$.getJSON(reddit_api_url,function(data) {
  $.each(data.children['data'], function(i, reddit) {
    // Uncomment line below to show tweet data in Fire Bug console
    // Very helpful to find out what is available in the tweet objects
  //  console.log(reddit);

    // Before we continue we check that we got data

        console.log(i+' : '+ reddit );
      // Build the html string for the current tweet
      var tweet_html = reddit.url;


      // Append html string to tweet_container div
      $('#tweet_container').append(tweet_html);

  });
}

Thanks

ntziolis
  • 10,091
  • 1
  • 34
  • 50
damniatx
  • 234
  • 6
  • 20

1 Answers1

0

The data returned has its own data property, so you'd need...

data.data.children[0]['data']

...to get to that innermost data.

If you were iterating the children, then you'd pass that to $.each...

$.each(data.data.children, function(i, obj) {
    console.log(obj.data.url);
});
  • it still can't map the data. i'm sure i already trying that. here is the complete data : http://pastebin.com/q9JuqXHp – damniatx Mar 10 '12 at 16:52
  • @damniatx: Describe specifically the output you want. –  Mar 10 '12 at 16:55
  • How do i get the url From Json data above ?, there is no url on reddit.url – damniatx Mar 10 '12 at 16:58
  • @damniatx: You want the url from each of the children, or just one? –  Mar 10 '12 at 16:59
  • each of the url to be exact. sorry. – damniatx Mar 10 '12 at 17:00
  • @damniatx: I updated my answer to log each url to the console. [Here's an example](http://jsfiddle.net/ACBbY/) that writes it to the document. –  Mar 10 '12 at 17:06
  • i still can't find the url data, but i can accept your answer. thanks for your help. – damniatx Mar 10 '12 at 17:34
  • @damniatx: Have you verified that the data is being received? Strange that it wouldn't work. –  Mar 10 '12 at 17:36
  • i'm surely don't know how to do that, this is my full html code, http://pastebin.com/RuLnGwmv – damniatx Mar 10 '12 at 17:39
  • @damniatx: You'd verify by putting a `console.log(data)` before the `$.each(...` call, but I think I see the issue. You're requesting data from a different domain. It violates the security policy that browsers enforce called *Same Origin Policy*. A way around it is to make a `jsonp` request, but only if `reddit` supports it. I'm doing some looking right now. –  Mar 10 '12 at 17:46
  • @damniatx: I think I've got it... Change the url to `'http://api.reddit.com/r/pics.json?jsonp=?'`. I found it in [this answer](http://stackoverflow.com/questions/8191105/how-to-extract-url-data-from-reddit-api-using-json). See it working [in this demo](http://jsfiddle.net/7gv9b/). –  Mar 10 '12 at 17:50
  • 1
    oh man, that's worked on me. how do i say thanks to you ?, thank you very much! – damniatx Mar 10 '12 at 17:54
  • 1
    i still can't believe you helped me until this far. – damniatx Mar 10 '12 at 17:58