2011-10-29 25 views
0

我有一個管道JSON提要@http://pipes.yahoo.com/pipes/pipe.run?_id=32188be96d1caf97c46a6fcfd8c00820&_render=json衰竭JSON

{ 
"count":4, 
"value":{ 
    "title":"Copy of Twitter Farm #talkdesign Pipe", 
    "description":"Pipes Output", 
    "link":"http:\/\/pipes.yahoo.com\/pipes\/pipe.info?_id=32188be96d1caf97c46a6fcfd8c00820", 
    "pubDate":"Sat, 29 Oct 2011 05:40:41 -0700", 
    "generator":"http:\/\/pipes.yahoo.com\/pipes\/", 
    "callback":"", 
    "items":[ 
    { 
     "link":"http:\/\/twitter.com\/talk_design\/statuses\/130251640623214592", 
     "description":"talk_design: Pretty cool interactive music video for Ellie Goulding's Lights! <a rel=\"nofollow\" target=\"_blank\" href=\"http:\/\/search.twitter.com\/search?q=%23webgl\" title=\"#webgl\" class=\" \">#webgl<\/a> <a rel=\"nofollow\" target=\"_blank\" href=\"http:\/\/t.co\/kkA3ON09\">http:\/\/t.co\/kkA3ON09<\/a>", 
     "pubDate":"Sat, 29 Oct 2011 11:56:25 +0000", 
     "guid":"http:\/\/twitter.com\/talk_design\/statuses\/130251640623214592", 
     "author":"[email protected] (Talk.Design)", 
     "media:content":{ 
      "height":"48", 
      "type":"image\/jpg", 
      "url":"http:\/\/a1.twimg.com\/profile_images\/1611029053\/iphone-4-icon_normal.png", 
      "width":"48" 
     }, 
     "google:image_link":"http:\/\/a1.twimg.com\/profile_images\/1611029053\/iphone-4-icon_normal.png", 
     "twitter:metadata":{ 
      "twitter:result_type":"recent" 
     }, 
     "y:published":{ 
      "hour":"11", 
      "timezone":"UTC", 
      "second":"25", 
      "month":"10", 
      "month_name":"October", 
      "minute":"56", 
      "utime":"1319889385", 
      "day":"29", 
      "day_ordinal_suffix":"th", 
      "day_of_week":"6", 
      "day_name":"Saturday", 
      "year":"2011" 
     }, 
     "y:id":{ 
      "permalink":"false", 
      "value":"http:\/\/twitter.com\/talk_design\/statuses\/130251640623214592" 
     }, 
     "y:title":"talk_design: Pretty cool interactive music video for Ellie Goulding's Lights! #webgl http:\/\/t.co\/kkA3ON09", 
     "title":"We Said: Pretty cool interactive music video for Ellie Goulding's Lights! #webgl http:\/\/t.co\/kkA3ON09" 
    }, 
    { 
     "link":"http:\/\/twitter.com\/OwenMelbz\/statuses\/129989645940428800", 
     "description":"OwenMelbz: @<em>talk_design<\/em> #<em>talk_design<\/em> test test test", 
     "pubDate":"Fri, 28 Oct 2011 18:35:21 +0000", 
     "guid":"http:\/\/twitter.com\/OwenMelbz\/statuses\/129989645940428800", 
     "author":"[email protected] (Owen Melbourne)", 
     "media:content":{ 
      "height":"48", 
      "type":"image\/jpg", 
      "url":"http:\/\/a1.twimg.com\/profile_images\/702379978\/DSC01872222_normal.jpg", 
      "width":"48" 
     }, 
     "google:image_link":"http:\/\/a1.twimg.com\/profile_images\/702379978\/DSC01872222_normal.jpg", 
     "twitter:metadata":{ 
      "twitter:result_type":"recent" 
     }, 
     "y:published":{ 
      "hour":"18", 
      "timezone":"UTC", 
      "second":"21", 
      "month":"10", 
      "month_name":"October", 
      "minute":"35", 
      "utime":"1319826921", 
      "day":"28", 
      "day_ordinal_suffix":"th", 
      "day_of_week":"5", 
      "day_name":"Friday", 
      "year":"2011" 
     }, 
     "y:id":{ 
      "permalink":"false", 
      "value":"http:\/\/twitter.com\/OwenMelbz\/statuses\/129989645940428800" 
     }, 
     "y:title":"OwenMelbz: @talk_design #talk_design test test test", 
     "title":"OwenMelbz: #talkdesign test test test" 
    }, 
    { 
     "link":"http:\/\/twitter.com\/talk_design\/statuses\/129988795574657024", 
     "description":"talk_design: Video: Jay Bradner: Open-source cancer researchFrom: TEDtalksDirector | Oct 27, 2011 | 7,265... <a rel=\"nofollow\" target=\"_blank\" href=\"http:\/\/t.co\/5hrxfCHC\">http:\/\/t.co\/5hrxfCHC<\/a>", 
     "pubDate":"Fri, 28 Oct 2011 18:31:58 +0000", 
     "guid":"http:\/\/twitter.com\/talk_design\/statuses\/129988795574657024", 
     "author":"[email protected] (Talk.Design)", 
     "media:content":{ 
      "height":"48", 
      "type":"image\/jpg", 
      "url":"http:\/\/a1.twimg.com\/profile_images\/1611029053\/iphone-4-icon_normal.png", 
      "width":"48" 
     }, 
     "google:image_link":"http:\/\/a1.twimg.com\/profile_images\/1611029053\/iphone-4-icon_normal.png", 
     "twitter:metadata":{ 
      "twitter:result_type":"recent" 
     }, 
     "y:published":{ 
      "hour":"18", 
      "timezone":"UTC", 
      "second":"58", 
      "month":"10", 
      "month_name":"October", 
      "minute":"31", 
      "utime":"1319826718", 
      "day":"28", 
      "day_ordinal_suffix":"th", 
      "day_of_week":"5", 
      "day_name":"Friday", 
      "year":"2011" 
     }, 
     "y:id":{ 
      "permalink":"false", 
      "value":"http:\/\/twitter.com\/talk_design\/statuses\/129988795574657024" 
     }, 
     "y:title":"talk_design: Video: Jay Bradner: Open-source cancer researchFrom: TEDtalksDirector | Oct 27, 2011 | 7,265... http:\/\/t.co\/5hrxfCHC", 
     "title":"We Said: Video: Jay Bradner: Open-source cancer researchFrom: TEDtalksDirector | Oct 27, 2011 | 7,265... http:\/\/t.co\/5hrxfCHC" 
    }, 
    { 
     "link":"http:\/\/twitter.com\/talk_design\/statuses\/129986263011950592", 
     "description":"talk_design: setting up website and facebook :)", 
     "pubDate":"Fri, 28 Oct 2011 18:21:54 +0000", 
     "guid":"http:\/\/twitter.com\/talk_design\/statuses\/129986263011950592", 
     "author":"[email protected] (Talk.Design)", 
     "media:content":{ 
      "height":"48", 
      "type":"image\/jpg", 
      "url":"http:\/\/a1.twimg.com\/profile_images\/1611029053\/iphone-4-icon_normal.png", 
      "width":"48" 
     }, 
     "google:image_link":"http:\/\/a1.twimg.com\/profile_images\/1611029053\/iphone-4-icon_normal.png", 
     "twitter:metadata":{ 
      "twitter:result_type":"recent" 
     }, 
     "y:published":{ 
      "hour":"18", 
      "timezone":"UTC", 
      "second":"54", 
      "month":"10", 
      "month_name":"October", 
      "minute":"21", 
      "utime":"1319826114", 
      "day":"28", 
      "day_ordinal_suffix":"th", 
      "day_of_week":"5", 
      "day_name":"Friday", 
      "year":"2011" 
     }, 
     "y:id":{ 
      "permalink":"false", 
      "value":"http:\/\/twitter.com\/talk_design\/statuses\/129986263011950592" 
     }, 
     "y:title":"talk_design: setting up website and facebook :)", 
     "title":"We Said: setting up website and facebook :)" 
    } 
    ] 
} 
} 

但是對我的生活我不能建立我的字符串,我想。我正在使用的「理論」是。

while i <= json[count]{ //open loop using json count data 
    tweet = json[items[i]][description]; //get the tweet 
    tweet = tweet.replace(/\/g,""); //remove slashes 
    by = json[items[i]][author]; //get author 
    by = by.split("("); //ignore full name just get short 
    by = by[1]; // shortname 
    by = by.replace(/)/g,""); //remove last) 
    avatar = json[items[i]][media:content][url]; //get avatar url 
    avatar = avatar.replace(/\/g, "") //remove slashes 
    tweet = "<img src=""+avatar+"" width=\"16\" height=\"16\" />"+author+": "+tweet; //format the tweet 
    $("#tweetarea").append(tweet); //print it to #tweetarea 
    i++ //update counter 
} //close loop 

但老實說,我不能弄清楚這個json百靈。任何人都願意編寫jquery代碼來解析這個json。並執行我上面描述的行動?

謝謝:)

+1

試圖以精確和整齊的格式來組織你的問題,這將幫助你得到你的答案先生... – abhijit

+0

這是更好嗎?謝謝 – Owen

回答

1

你可以使用jQuery的each()方法輕鬆具體鳴叫數據遍歷JSON和追加到#tweetarea

$.each(json.value.items, function(i, tweet) { 
     var author = tweet.author.split('(')[1].replace(')',''); 
     var avatar = tweet['media:content'].url; 
     $('#tweetarea').append('<img src="'+avatar+'" width="16" height="16" />'+author+': '+tweet['y:title']+'<br/>'); 
}); 

看到一個Working demo

編輯:

您可以通過jQuery的加載JSON 10方法。這是一個updated demo

+0

啊,這是輝煌的謝謝,但我怎麼去加載到var json飼料然而呢? – Owen

1

使用jQuery AJAX API w /設置爲JSONP。類似這樣的:

$.ajax({ 
    url: 'yahoourlhere?callback=?' 
    dataType: 'jsonp', 
}).success(function(response, status, jqxhr) { 

}).error(function(jqxhr, status, error) { 

}); 

你需要看看是否可以得到纏繞PIPE飼料的回調名稱。這是你將如何包裝JSON的JSONP調用PHP例子:

<?php 
    $callback = $_REQUEST['callback']; 
    echo "$callback\($json\)"; 
?>