gpt4 book ai didi

javascript - AngularJS - Restheart API (MongoDB) - 解析 JSON 结果

转载 作者:行者123 更新时间:2023-12-02 15:51:22 25 4
gpt4 key购买 nike

我正在使用 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"
}
]

}, _链接:{

  },
_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格式,看看resource representation section有关更多信息,请参阅 Restheart 文档。

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

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

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

如果您还传递 count 查询参数,您将获得 _size_total_pages 属性。

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

关于javascript - AngularJS - Restheart API (MongoDB) - 解析 JSON 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31833200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com