Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 03:20 PM   PM User | #1
erebus
New Coder

 
Join Date: Sep 2012
Location: Houston, TX
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
erebus is an unknown quantity at this point
Parsing JSON - Finding element paths

I've gotten some help with working with my JSON replies... however im having alot of troubles determining the right path to elements of the reply. Is there a simplier way or perhaps a software that will better layout the JSON reply so i can see the objects more clearly?

For example, I'm getting a JSON reply from the youtube API and im trying to get the path to the first video in the list of 3 ive requested... i think it's something like this...

title = base.entry[0].title
link = base.entry[0].link[0].href
thumbnail = base.entry[0].media$thumbnail[0].url
info = base.entry[0].media$description.$t

am i in the right ballpark on these refrences?

the JSON reply can be pulled up here as it appears to be to long to paste into a code bracket

https://gdata.youtube.com/feeds/api/...3&v=2&alt=json
erebus is offline   Reply With Quote
Old 10-10-2012, 03:35 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you can plug that url in here which makes it a bit more humanly readable
xelawho is offline   Reply With Quote
Old 10-10-2012, 04:29 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
looking at it, I think the last two would be better one level deeper, into the media$group object, like this:

Code:
var thumbnail = base.entry[0].media$group.media$thumbnail[0].url;
var info = base.entry[0].media$group.media$description.$t;
xelawho is offline   Reply With Quote
Old 10-10-2012, 04:58 PM   PM User | #4
erebus
New Coder

 
Join Date: Sep 2012
Location: Houston, TX
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
erebus is an unknown quantity at this point
Quote:
Originally Posted by xelawho View Post
you can plug that url in here which makes it a bit more humanly readable
That is bookmarked and a great new reference, thank you very very much for that... my god just a few moments looking at that format and i had it all figured out,

Wrote the callback and function and i was dead on using the link you gave me!

If your interested at all or anyone out there would like some youtue API reference material here is the results

Callback:

Code:
<script 
    type="text/javascript" 
    src="https://gdata.youtube.com/feeds/api/videos?q=dayz&category=gaming&orderby=viewCount&max-results=3&v=2&alt=json-in-script&callback=showMyVideos">
</script>
Function:

Code:
function showMyVideos(data)
{
   var feed = data.feed;
   var base = feed.entry;
   for (var i = 0; i < base.length; i ++ )
   {
      document.getElementById("videologo" + (i + 1)).src = base[i].media$group.media$thumbnail[0].url;
      document.getElementById("videoname" + (i + 1)).innerHTML = base[i].title.$t;
      var status = base[i].media$group.media$description.$t;
      if (status.length > 76)
      {
      var statusubstr = status.substr(0,76)
      document.getElementById("videoinfo" + (i + 1)).innerHTML = "<p>" + statusubstr + "... </p>";
      }
      else
      {
      document.getElementById("videoinfo" + (i + 1)).innerHTML = "<p>" + status + " </p>";
      }
   }
}
erebus is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:25 PM.


Advertisement
Log in to turn off these ads.