我正在使用RestHeart公开来自MongoBD的CRUD操作。并尝试从AngularJS调用Rest API并获取JSON结果,如下面的JSON字符串所示。但是我只对存储在MongoDB中的名称,年龄和城市字段感兴趣。

我不确定如何获得这些价值。

JavaScript代码:-

crudApp.controller('listController', function($scope, $http, $location,
    crudService) {

$http.get('http://localhost:8081/jaydb/employees').success(
        function(response) {
            console.log('response : ' +JSON.stringify(response));
            $scope.employees = response;
        });


})

REST API的JSON结果

{
_embedded: {
rh: doc: [
  {
    _embedded: {

    },
    _links: {
      self: {
        href: "/jaydb/employees/55c1e7c41c49a8cd78818bc7"
      },
      rh: coll: {
        href: "/jaydb"
      },
      curies: [
        {
          href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
          name: "rh"
        }
      ]
    },
    _type: "DOCUMENT",
    _id: {
      $oid: "55c1e7c41c49a8cd78818bc7"
    },



    name: "Anupama",
    city: "Trichy",
    age: 25,



    _etag: {
      $oid: "55c1e7c41c49a8cd78818bc8"
    },
    _lastupdated_on: "2015-08-05T10:39:00Z",
    _created_on: "2015-08-05T10:39:00Z"
  },
  {
    _embedded: {

    },
    _links: {
      self: {
        href: "/jaydb/employees/55c1e7ae1c49a8cd78818bc5"
      },
      rh: coll: {
        href: "/jaydb"
      },
      curies: [
        {
          href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
          name: "rh"
        }
      ]
    },
    _type: "DOCUMENT",
    _id: {
      $oid: "55c1e7ae1c49a8cd78818bc5"
    },
name: "Sujatha",
city: "Chennai",
age: 24,
    _etag: {
      $oid: "55c1e7ae1c49a8cd78818bc6"
    },
    _lastupdated_on: "2015-08-05T10:38:38Z",
    _created_on: "2015-08-05T10:38:38Z"
  },
  {
    _embedded: {

    },
    _links: {
      self: {
        href: "/jaydb/employees/55c1e7981c49a8cd78818bc3"
      },
      rh: coll: {
        href: "/jaydb"
      },
      curies: [
        {
          href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
          name: "rh"
        }
      ]
    },
    _type: "DOCUMENT",
    _id: {
      $oid: "55c1e7981c49a8cd78818bc3"
    },



    name: "Soniya",
    city: "Ernakulam",
    age: 22,



    _etag: {
      $oid: "55c1e7981c49a8cd78818bc4"
    },
    _lastupdated_on: "2015-08-05T10:38:16Z",
    _created_on: "2015-08-05T10:38:16Z"
  },
  {
    _embedded: {

    },
    _links: {
      self: {
        href: "/jaydb/employees/55c1e7711c49a8cd78818bc1"
      },
      rh: coll: {
        href: "/jaydb"
      },
      curies: [
        {
          href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
          name: "rh"
        }
      ]
    },
    _type: "DOCUMENT",
    _id: {
      $oid: "55c1e7711c49a8cd78818bc1"
    },



    name: "Reshma",
    city: "Trivandrum",
    age: 21,



    _etag: {
      $oid: "55c1e7711c49a8cd78818bc2"
    },
    _lastupdated_on: "2015-08-05T10:37:37Z",
    _created_on: "2015-08-05T10:37:37Z"
  },
  {
    _embedded: {

    },
    _links: {
      self: {
        href: "/jaydb/employees/55c1d3a8b216e0710f8ee0ab"
      },
      rh: coll: {
        href: "/jaydb"
      },
      curies: [
        {
          href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
          name: "rh"
        }
      ]
    },
    _type: "DOCUMENT",
    _id: {
      $oid: "55c1d3a8b216e0710f8ee0ab"
    },



     name: "Michael",
     city: "Tokyo",
     age: 23,



    _created_on: "2015-08-05T09:13:12Z"
  }
]


},
      _links:{

  },
  _type: "COLLECTION",
  _id: "employees",
  _created_on: "2015-08-05T09:38:36Z",
  _etag: {
    $oid: "55c1d99c1c49a8cd78818bb6"
  },
  _lastupdated_on: "2015-08-05T09:38:36Z",
  _collection-props-cached: false,
  _returned: 5
}


参考:http://restheart.org/docs/walkthrough.html

最佳答案

RESTHeart使用HAL格式,请参阅restheart文档的resource representation section以获取更多信息。

总而言之,您的请求是对集合资源的GET:响应包括集合在第一层的自身属性以及作为嵌入式资源的文档。

您可以通过包含数组rh:doc;的_embedded属性访问集合的文档。该数组的元素是您的文档。

另请注意,文档是分页的:默认情况下,RESTHeart返回第一个(最多)1000个文档。您可以通过page和pagesize查询参数控制分页。

如果您还传递count查询参数,则将获得_size和_total_pages属性。

_links参数包括指向下一页和上一页的下一个以及最终的上一个链接。

10-08 19:22