为facebook帖子解析巨大的JSON对象

时间:2014-10-11 23:27:09

标签: ruby-on-rails angularjs facebook-graph-api

我目前正在建立一个angular.js社交媒体平台,我正在尝试在Facebook的Feed页面上显示Facebook帖子。我目前有这个:

<div id='fb-root'>
    <div ng-repeat="post in facebook">
        Test: {{post}}
    </div>
</div>

将我的测试帖子返回为:

[{&#34; ID&#34;:&#34; 4681211244366_4681199684077&#34;&#34;从&#34; {&#34; ID&#34;:&#34; 4681211244366&#34; ,&#34;姓名&#34;:&#34; Zachy Bear Jones&#34;},&#34;故事&#34;:&#34; Zachy Bear Jones和Alexander J. Marrs现在是朋友。&#34 ;,&#34; story_tags&#34; {&#34; 0&#34;:[{&#34; ID&#34;:&#34; 4681211244366&#34;&#34;名称&#34 ;: &#34; Zachy Bear Jones&#34;,&#34; offset&#34;:0,&#34; length&#34;:16,&#34; type&#34;:&#34; user&#34; },&#34; 21&#34;:[{&#34; id&#34;:&#34; 10205311345929912&#34;,&#34; name&#34;:&#34; Alexander J. Marrs&# 34;,&#34;偏移&#34;:21,&#34;长度&#34;:18,&#34;类型&#34;:&#34;使用者&#34;}]},&#34;动作&#34;:[{&#34;名称&#34;:&#34;注释&#34;&#34;连结&#34;:&#34; https://www.facebook.com/4681211244366/posts/4681199684077&#34;},{ &#34;名称&#34;:&#34;像&#34;&#34;连结&#34;:&#34; https://www.facebook.com/4681211244366/posts/4681199684077&#34;}],&#34;隐私&#34 ;:{&#34;值&#34;:&#34;&#34;}&#34;类型&#34;:&#34;状态&#34;&#34; status_type&#34;:& #34; approved_friend&#34;&#34; CREATED_TIME&#34;:&#34; 2014-10-11T21:33:29 + 0000&#34;&#34; updated_time&#34;:&#34; 2014 -10-11T21:33:29 + 0000&#34;},{&#34; ID&#34; :&#34; 4681211244366_4678763463173pmpmymife&#34;&#34;从&#34; {&#34; ID&#34;:&#34; 4681211244366&#34;&#34;名称&#34;:&#34 ; Zachy Bear Jones&#34;},&#34;故事&#34;:&#34; Zachy Bear Jones评论了一个状态。&#34;,&#34; story_tags&#34;:{&#34; 0& #34;:[{&#34; id&#34;:&#34; 4681211244366&#34;,&#34; name&#34;:&#34; Zachy Bear Jones&#34;,&#34; offset&# 34;:0,&#34;长度&#34;:16,&#34;类型&#34;:&#34;使用者&#34;}]},&#34;隐私&#34; {&#34 ;值&#34;:&#34;&#34;}&#34;类型&#34;:&#34;状态&#34;&#34;应用&#34; {&#34;名称&# 34;:&#34; Facebook for iPhone&#34;,&#34; namespace&#34;:&#34; fbiphone&#34;,&#34; id&#34;:&#34; 6628568379&#34;} &#34; CREATED_TIME&#34;:&#34; 2014-10-11T08:20:12 + 0000&#34;&#34; updated_time&#34;:&#34; 2014-10-11T08:20: 12 + 0000&#34;},{&#34; ID&#34;:&#34; 4681211244366_4676849935336&#34;&#34;从&#34; {&#34; ID&#34;:&#34; 4681211244366&#34;,&#34;名称&#34;:&#34; Zachy Bear Jones&#34;},&#34;故事&#34;:&#34; Zachy Bear Jones喜欢照片。&#34; &#34; story_tags&#34; {&#34; 0&#34;:[{&#34; ID&#34;:&#34; 46812 11244366&#34;,&#34; name&#34;:&#34; Zachy Bear Jones&#34;,&#34; offset&#34;:0,&#34; length&#34;:16,&#34 ;类型&#34;:&#34;使用者&#34;}]},&#34;隐私&#34; {&#34;值&#34;:&#34;&#34;}&#34 ;类型&#34;:&#34;状态&#34;&#34; CREATED_TIME&#34;:&#34; 2014-10-10T18:35:16 + 0000&#34;&#34; updated_time&#34 ;:&#34; 2014-10-10T18:35:16 + 0000&#34;},{&#34; ID&#34;:&#34; 4681211244366_4676161038114&#34;&#34;从&#34 ;: {&#34; id&#34;:&#34; 4681211244366&#34;,&#34; name&#34;:&#34; Zachy Bear Jones&#34;},&#34; story&#34;:& #34; Zachy Bear Jones喜欢The ManKind Project。&#34;,&#34; story_tags&#34;:{&#34; 0&#34;:[{&#34; id&#34;:&#34; 4681211244366&#34;,&#34; name&#34;:&#34; Zachy Bear Jones&#34;,&#34; offset&#34;:0,&#34; length&#34;:16,&#34 ;类型&#34;:&#34;使用者&#34;}],&#34; 23&#34;:[{&#34; ID&#34;:&#34; 95845568627&#34;&#34; name&#34;:&#34; ManKind Project&#34;,&#34; offset&#34;:23,&#34; length&#34;:19,&#34; type&#34;:&#34 ;页面&#34;}]},&#34;图像&#34;:&#34; https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/v/t1.0-1/c25.37.310.310/s100x100/47037_10151832647868628_1954427594_n.png

返回我的JSON对象。将此信息呈现为帖子的下一步是什么?在完美的世界中,我需要个人资料图片,消息,时间,位置等,共享和评论操作。有关如何做到这一点的任何建议?谢谢任何可以提供帮助的人!

1 个答案:

答案 0 :(得分:1)

首先,您应该将json对象格式化为可读的对象以获得特定结构。根据你的对象,我做了一个例子:

[{
    "id":"4681211244366_4681199684077",
    "from":{
        "id":"4681211244366",
        "name":"Zachy Bear Jones"
    },
    "story":"Zachy Bear Jones and Alexander J. Marrs are now friends.",
    "story_tags":{
        "0": [{
            "id":"4681211244366",
            "name":"Zachy Bear Jones",
            "offset":0,
            "length":16,
            "type":"user"
        }],
        "21":[{
            "id":"10205311345929912",
            "name":"Alexander J. Marrs",
            "offset":21,
            "length":18,
            "type":"user"
        }]
    },
    "actions":[{
        "name":"Comment",
        "link":"https://www.facebook.com/4681211244366/posts/4681199684077"
    },{
        "name":"Like",
        "link":"https://www.facebook.com/4681211244366/posts/4681199684077"
    }],
    "privacy":{
        "value":""
    },
    "type":"status",
    "status_type":"approved_friend",
    "created_time":"2014-10-11T21:33:29+0000",
    "updated_time":"2014-10-11T21:33:29+0000"
},
...

要显示数据,请使用以下内容:

<div id='fb-root'>
    <div ng-repeat="post in facebook">
        Post from: {{post.from.name}} <br />
        Story: {{post.story}} <br />
        Tagged: <span ng-repeat="tag in post">{{tag.name}}</span>
    </div>
</div>

这应该是这样的:

<div id='fb-root'>
    <div>
        Post from: Zachy Bear Jones <br />
        Story: Zachy Bear Jones and Alexander J. Marrs are now friends. <br />
        Tagged: <span>Zachy Bear Jones</span><span>Alexander J. Marrs</span>
    </div>
</div>