gpt4 book ai didi

javascript - JS 中的 JSON 解析

转载 作者:行者123 更新时间:2023-12-02 19:34:10 25 4
gpt4 key购买 nike

我从服务器收到 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"
}
]
}
]
}

我想获取 children 的页面位置。

谁能告诉我该怎么做?

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

而且当我获取索引数组时,它只返回我,而不是索引号。我正在使用以下代码:

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

感谢和问候

最佳答案

如果您有兴趣简单地检索所有页面位置,您可以使用 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]]

关于javascript - JS 中的 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11132498/

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