繁体   English   中英

JS中的JSON解析

[英]JSON parsing in JS

我从服务器得到一个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; }) 

感谢和问候

如果您只想检索所有页面位置,可以使用过滤器执行此操作:

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]]

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

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

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

将为您提供第一页的位置..依此类推

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM