2011-06-10 48 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