多个返回的密钥json

use*_*428 1 javascript angularjs angularjs-factory angularjs-http ionic-framework

我有一个运行k2的joomla网站,并通过在字符串末尾添加format = json来从中提取信息:https://www.example.com/posts? format = json ,输出如下内容:

{
site: {
      url: "https://www.example.com",
      name: "mySite"
      },
category: {
          id: "67",
          name: "Gauteng",
          alias: "gauteng",
          link: "/tna/provincial/gauteng.html",
          parent: "66",
          extraFieldsGroup: "0",
          image: null,
          ordering: "1",
        events: {
          K2CategoryDisplay: ""
        },
        chidlren: []
       },
items: [{ 
         id="1",
         title="The Title",
         body="body content here..."
        },
        { 
         id="2",
         title="The Title",
         body="body content here..."
        }
}
Run Code Online (Sandbox Code Playgroud)

现在我正在为此创建一个服务,只想访问"项目".

.factory('Posts', function($http) {

  var posts = [];

  return {
        getPosts: function(){
            return $http.get("https://www.example.com/posts?format=json").then(function(response){
                posts = response;
                return posts;
            });
        },
        getPost: function(index){
            return posts[index];
        }
    }

});
Run Code Online (Sandbox Code Playgroud)

它似乎没有工作.有没有办法只通过电话访问"项目"?

Pan*_*kar 5

你应该发布语法 posts = response.data.items

return $http.get("https://www.example.com/posts?format=json").then(function(response){
    posts = response.data.items; //items here
    return posts;
});
Run Code Online (Sandbox Code Playgroud)