2016-02-12 49 views
0

我正在使用AngularJS的Yammer RESTful API。我已經能夠讓yammer user.json api工作,但需要指導消費和顯示yammer messages.json api。如果有人可以幫助使用範圍語法,那很酷。如何使用Angular.js解析json數組對象?

解析JSON返回的消息>身體>豐富 下面是代碼:

控制器

function YammerGetUserCtrl($scope, $http) { 
$http.get('https://api.yammer.com/api/v1/messages.json', {headers: {'Authorization': 'Bearer xxxxxxxxxxxxxxx'}}). 
     success(function(data) { 
     $scope.users = data; 
     console.log($scope.users) 
    }); 
} 

JSON

{ 
     "threaded_extended": {}, 
     "messages": [ 
      { 
       "id": 654897910, 
       "sender_id": 1506696042, 
       "replied_to_id": null, 
       "created_at": "2016/02/12 20:55:02 +0000", 
       "network_id": 11319, 
       "message_type": "update", 
       "sender_type": "user", 
       "url": "https://www.yammer.com/api/v1/messages/654897910", 
       "web_url": "https://www.yammer.com/arrow.com/messages/654897910", 
       "body": { 
        "urls": [ 
         "https://www.youtube.com/watch?v=yueP7V6Wddc&index=7&list=PLKsggbHA8DermEtbMcC80M38cuR5rFdgi" 
        ], 
        "parsed": "Our shiny new aerospace [[tag:8909387]] that ran in Colorado markets during Super Bowl 50 is going viral! In less than one week since broadcast, we have more than 414,000 views on YouTube, increasing by thousands an hour. The [[tag:8898375]] has caught media attention around the world in the UK, Ireland, Australia, Germany and more. Even mention by MIT Media Lab.\n\nhttps://www.youtube.com/watch?v=yueP7V6Wddc&index=7&list=PLKsggbHA8DermEtbMcC80M38cuR5rFdgi", 
        "plain": "Our shiny new aerospace #ad that ran in Colorado markets during Super Bowl 50 is going viral! In less than one week since broadcast, we have more than 414,000 views on YouTube, increasing by thousands an hour. The #video has caught media attention around the world in the UK, Ireland, Australia, Germany and more. Even mention by MIT Media Lab.\n\nhttps://www.youtube.com/watch?v=yueP7V6Wddc&index=7&list=PLKsggbHA8DermEtbMcC80M38cuR5rFdgi", 
        "rich": "Our shiny new aerospace <span class='yammer-object' data-yammer-object='tag:8909387' data-resource-id='8909387' data-resource-model='tag'>#<a href='https://www.yammer.com/arrow.com/topics/11782336'>ad</a></span> that ran in Colorado markets during Super Bowl 50 is going viral! In less than one week since broadcast, we have more than 414,000 views on YouTube, increasing by thousands an hour. The <span class='yammer-object' data-yammer-object='tag:8898375' data-resource-id='8898375' data-resource-model='tag'>#<a href='https://www.yammer.com/arrow.com/topics/8400514'>video</a></span> has caught media attention around the world in the UK, Ireland, Australia, Germany and more. Even mention by MIT Media Lab.<br><br><a class=\"linkified\" href=\"https://www.youtube.com/watch?v=yueP7V6Wddc&amp;index=7&amp;list=PLKsggbHA8DermEtbMcC80M38cuR5rFdgi\" title=\"https://www.youtube.com/watch?v=yueP7V6Wddc&amp;index=7&amp;list=PLKsggbHA8DermEtbMcC80M38cuR5rFdgi\" target=\"_blank\" 
      ..... 
    } 

所以我想顯示的URL和豐富的摘錄從身體。我該怎麼做?

回答

2

編輯,這裏是AngularJS解決方案。

$http.get("your_data").success(function (data) 
{ 
    $scope.users = data.messages; 
    console.log("users" , $scope.users); 
}); 

和HTML

<div ng-repeat="user in users" > 
    <div>Rich: {{user.body.rich}}</div> 
    <div>URL: {{user.url}}</div> 
</div> 

要直接訪問rich屬性,您可以使用此代碼:user.messages[0].body["rich"]url的說明類似:說明:消息具有包含一個元素的數組,以及具有屬性rich的對象body。在控制檯中進行調試後,查看下面的結果。 Console view of the JSON

+0

iulia,非常好。非常感謝。我現在會嘗試。讓你知道在幾個.... – Mike

+0

嗯,我相信這是工作,我看到了JSON響應,但我認爲我的Htnl代碼是錯誤的。我試過​​{{user.messages}}​​{{user.messages.body.rich}}​​{{user.rich}} ...任何意見?謝謝 – Mike

+0

嗨,是的,這裏是我的想法在AngularJS形式:'$ http.get(「your_data」)。成功(功能(數據){scope.users = data.messages; console.log(「users 「,$ scope.users); });' 和HTML'

Rich: {{user.body.rich}}
URL: {{user.url}}
'當然,您可以在HTML中設置樣式。 – iulia