2011-06-10 26 views
0

我試圖檢索有關用戶輸入url的facebook事件的詳細信息。與AJAX處理Facebook JSON

基本上,用戶輸入事件的URL例如。 https://www.facebook.com/event.php?eid=102036779889416 然後生成的JSON格式的信息將被分配給變量並傳遞到適當的表單值,它只是一個自動填充功能,使我的網站更簡單。

現在我必須將URL傳遞文件的形式,我使用的getJSON

$.getJSON("getevent.php?event="+event,function(data) 

但我不知道該怎麼做的回報,在例子中,我一直在下面中,JSON被格式化爲例如:

{"posts": 
[ 
{ 
"title":"9lessons | Programming Blog", 
"url":"http://9lessons.blogspot.com" 
}, 
{ 
"title":"jQuery and Ajax Demos Pard - 3", 
"url":"jquery-and-ajax-best-demos-part-3.html" 
}, 
] 
} 

,並且可以輸出,當它返回使用該:

<script type="text/javascript"> 
$(function() 
{ 
$(document).ready(function() 
{ 
$.getJSON("data.js",function(data) 
{ 
$.each(data.posts, function(i,data) 
{ 
var div_data = 
"<div ><a href='"+data.url+"'>"+data.title+"</a></div>"; 
$(div_data).appendTo("#9lessonsLinks"); 
}); 
} 
); 
return false; 
}); 
}); 
</script> 

但是,Facebook返回的JSON看起來像這樣;

{ 
    "id": "188892631122084", 
    "owner": { 
     "name": "Gerard Alonso", 
     "id": "742700474" 
    }, 
    "name": "M.E.T.H.O.D. 11.6.2011", 
    "description": "Yo! \n\nOld skool hip hop and other assorted jams at the Bodega. \n\nAll day jam with barbecue by Homemade, graffiti by Montana, Nottingham's best hip hop DJs and good vibes all round.\n\n\u00a33 entry all day and night, separate \u00a35 charge for Don't Flop rap battles upstairs http://www.facebook.com/event.php?eid=165044166891331.\n\n\nDJs line-up:\n\nTHE ELEMENTZ\nSQUIGZ\nBEATMASTER BILL\nSYNIC\nROOT ONE\nILLICIT & DESCRY\nADAM P\n+ more\n\n\nDuring the day:\n\n- Dont Flop rap battles upstairs (not included on the \u00a33 ticket price)\n- Mimm store >> http://tinyurl.com/3n8qltc\n- Jamaican BBQ (Homemade)\n- Funk and sunny hip hop in the downstairs bar\n\nIn the evening:\n\n- Old skool hip hop music upstairs along with plenty of mc's getting up in the mic\n- Drinks deals TBC\n- BBQ all day and night\n\n\nAny Mc's, dj's, breakers, graffiti artist get in touch. \n\nPeace, love and hip hop.", 
    "start_time": "2011-06-11T14:00:00", 
    "end_time": "2011-06-12T03:30:00", 
    "location": "The Bodega Social", 
    "privacy": "OPEN", 
    "updated_time": "2011-06-05T21:45:18+0000" 
} 

正如你可以看到它沒有頂部水平相當於「帖子」,我可以用,所以我是一個有點難倒。另外我不知道如何在文件輸出它,因爲我有這個在URL傳遞到PHP文件:

$url = $_GET['event']; 

parse_str(parse_url($url, PHP_URL_QUERY)); 
$id = $eid; 


$url = 'https://graph.facebook.com/'.$id; 

$json = file_get_contents($url,0,null,null); 
$json_output = json_encode($json, false); 


print_r($json_output); 

每次標價之前這似乎錯了,無論如何,作爲輸出滿反斜槓

我知道這很簡單,但我無法弄清楚。我想要做的就是將每個類別都分配給一個變量來處理它,但現在我只需要先輸出它。我發現AJAX非常複雜,因爲似乎有10種方法可以做同樣的事情。

編輯:我意識到我不需要JSONdecode,只是echo $出$ json。螢火蟲是看到了JSON產生罰款,但沒有什麼是增加這個到AJAX之後發生的事情:遠

$.getJSON("getevent.php?event="+event,function(data) 
{ 
var items = []; 

    $.each(data, function(key, val) { 
    items.push('<li id="' + key + '">' + val + '</li>'); 
    }); 

    $('<ul/>', { 
    'class': 'my-new-list', 
    html: items.join('') 
    }).appendTo('#arrData'); 
} 
); 
return false; 

,所以我不知道什麼即時試圖立即解決,因爲我可以做出來香港專業教育學院做了什麼手冊告訴我和JSON是正確的?

三江源

回答

0

我想你yrying使用JSON是帖子的列表。第二個JSON摘錄看起來像是正確的(即事件對象)。

+0

不,我認爲你有誤讀。第一個是我在下面的例子中使用的。第二個json是我想要使用的。我知道這是正確的,我只是不知道如何使用它。 – 2011-06-10 15:54:17

+0

它應該很好。我已經使用JavaScript代碼嘗試了JSON提取,並且它在列表中顯示了所有事件屬性。你是否檢查過getJSON收到的值,例如window.alert(data)? – tomconte 2011-06-10 21:32:23

+0

我在警告框中獲得[對象對象]? – 2011-06-11 08:24:48