0

我收到来自服务器的 JSON 响应:

{
    "width": "765",
    "height": "990",
    "srcPath": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_MERGED_/1273.pdf",
    "coverPage": "",
    "documents": [
        {
            "index": "1",
            "text": "Archiving Microsoft® Office SharePoint® Server 2007 Data with the Hitachi Content Archive Platform and Hitachi Data Discovery for Microsoft SharePoint",
            "type": "doc",
            "id": "HDS_054227~201106290029",
            "children": [
                {
                    "text": "Page 1",
                    "leaf": "true",
                    "pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png"
                },
                {
                    "text": "Page 2",
                    "leaf": "true",
                    "pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_2.png"
                }               

            ]
        },
        {
            "index": "11",
            "text": "Brocade FCoE Enabling Server I/O Consolidation",
            "type": "doc",
            "id": "HDS_053732~201105261741",
            "children": [
                {
                    "text": "Page 1",
                    "leaf": "true",
                    "pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_1.png"
                },
                {
                    "text": "Page 2",
                    "leaf": "true",
                    "pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_2.png"
                }
                     ]
        }
    ]
}

我想获得孩子们的页面位置。

谁能告诉我该怎么做?

嗨,我也想从中获取索引,然后想获取那个特定孩子的页面位置。你能告诉我我该怎么做吗?

而且当我得到索引数组时,它只返回我,而不是索引号。我为此使用以下代码:

  indexes=response.documents.map(function(e){ return e.children.index; }) 

感谢和问候

4

2 回答 2

2

如果您有兴趣简单地检索所有页面位置,可以使用filter来完成:

var locations = [];
json.documents.forEach(function(e,i) {
    e.children.forEach(function(e2,i2) {
        locations.push(e2.pageLocation);
    )}
});
// returns flat array like [item1,item2,item3,item4]

您可以使用map获取数组数组:

var locations = [];
var locations = json.documents.map(function(e) {
    return e.children.map(function(e2) {
        return e2.pageLocation;
    });
});

// returns 2-dimensional array like [[item1,item2],[item1,item2]]
于 2012-06-21T06:18:06.623 回答
0

您的 json 响应是一个适当的 javascript 对象,因此您可以像在后端一样访问对象的所有元素。在这里,您有一个文档类型的对象数组,每个文档对象都有一个子类型的对象数组。所以语法是

myjson.documents[0].children[0].pagelocation 

( = http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png)

会给你第一页的位置..等等

于 2012-06-21T06:20:42.063 回答