如何使用Angular.js解析json数组对象?

时间:2022-03-11 11:50:43

I am consuming a Yammer RESTful API with AngularJS. I have been able to get the yammer user.json api to work but need guidance consuming and displaying the yammer messages.json api. If anybody could help with the scope syntax, that would be cool.

我正在使用AngularJS使用Yammer RESTful API。我已经能够使yammer user.json api工作,但需要使用指导并显示yammer messages.json api。如果有人可以帮助使用范围语法,那将会很酷。

Parse json to return messages > body > rich Here is the code:

解析json返回消息> body> rich这是代码:

Controller

调节器

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

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\"
            .....
    }

So I would like to display the url and rich excerpt from the body. How can I do it?

所以我想展示身体的网址和丰富的摘录。我该怎么做?

1 个解决方案

#1


2  

Edit, here is the AngularJS solution.

编辑,这是AngularJS解决方案。

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

and in HTML

并以HTML格式

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

To directly access the rich property, you can use this code: user.messages[0].body["rich"]. Similar goes for url Explanation: messages has an array with one element, and the object body which has the property rich. See below my results after debugging in the console. 如何使用Angular.js解析json数组对象?

要直接访问rich属性,可以使用以下代码:user.messages [0] .body [“rich”]。类似于url说明:消息具有包含一个元素的数组,以及具有丰富属性的对象主体。在控制台中调试后,请参阅下面的结果。

#1


2  

Edit, here is the AngularJS solution.

编辑,这是AngularJS解决方案。

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

and in HTML

并以HTML格式

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

To directly access the rich property, you can use this code: user.messages[0].body["rich"]. Similar goes for url Explanation: messages has an array with one element, and the object body which has the property rich. See below my results after debugging in the console. 如何使用Angular.js解析json数组对象?

要直接访问rich属性,可以使用以下代码:user.messages [0] .body [“rich”]。类似于url说明:消息具有包含一个元素的数组,以及具有丰富属性的对象主体。在控制台中调试后,请参阅下面的结果。