开发者

Extracting data from a json sibling?

Some json from picasaweb: http://picasaweb.google.com/data/feed/api/user/100489095734859091829?kind=album&access=visible&alt=json-in-script&thumbsize=144c

Here's the output as tidied up by jsonview - these two both appear on the same "level", but several branches down the tree (I really don't "get" json yet - can you tell?!).

gphoto$id: {
$t: "5068801490268828641"
},
gphoto$name: {
$t: "ByronBay"
}

I want to say "given the gphoto$name 'ByronBay', I'd like its gphoto$id please!".

I found a couple of good jso开发者_开发问答n browsers; one online Java: http://pivot.apache.org/demos/json-viewer.html and a Firefox/Chrome extension: http://jsonview.com/

Still none the wiser though. The script I'm trying to use, http://oss.oetiker.ch/jquery/jquery.EmbedPicasaGallery.js, is great, but it demands an album ID from the user, which means opening the RSS and digging out the correct number. But I see in the returned json, that the album ID is a sibling of the album title.

I can find all sorts of other ways of getting "child" data from a given parent, but I'm really drawing a blank when it comes to sibling data like this.


The objects are both members of the same 'parent' json.feed.entry[index] , so you may walk through json.feed.entry and check gphoto$name.$t

$.getJSON('http://picasaweb.google.com/data/feed/api/user/'+
          '100489095734859091829?kind=album&access=visible&'+
          'alt=json&thumbsize=144c&callback=?', 
function(data) {
for(var i=0;i<data.feed.entry.length;++i)
{
  if(data.feed.entry[i].gphoto$name.$t=='ByronBay')
  {
    alert(data.feed.entry[i].gphoto$id.$t);return;
  }
}
});

or using jQuery's grep():

$.getJSON('http://picasaweb.google.com/data/feed/api/user/'+
          '100489095734859091829?kind=album&access=visible&'+
          'alt=json&thumbsize=144c&callback=?', 
function(data) {

  arr = jQuery.grep(jQuery.makeArray( data.feed.entry ), function(a){
        return (a.gphoto$name.$t=='ByronBay');
        });
  if(arr.length)
  {
    alert(arr[0].gphoto$id.$t);
  }
  else
  {
    alert('no match');
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜