从JSON数据中获取特殊值

时间:2018-10-26 08:21:09

标签: javascript json regex ajax openlayers-3

我正在通过javascript中的ajax调用获取数据:

$.ajax({
    type: "GET",
    url:URL,

    headers: {
                'Access-Control-Allow-Origin': '*'
            },
    async: false,
    dataType: "text",
    success: function (data) {
        result=data;
}

我们以这种方式得到答复:

{
    "type": "FeatureCollection",
    "totalFeatures": 3,
    "features": [{
        "type": "Feature",
        "id": "Site_view.5b59c3a5-bc04-4707-93e7",
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
                        [174877.4813, 130159.3695],
                        [174991.5857, 130162.2884],
                        [175085.7881, 130172.9028],
                        [175204.6689, 130195.7236],
                        [175205.9957, 130192.274],
                        [175084.4613, 130170.7799],
                        [174989.7282, 130158.8387],
                        [174877.4813, 130159.3695]
                    ]
                ],
                [
                    [
                        [175130.0918, 130397.2857],
                        [175152.5814, 130316.5877],
                        [174960.7581, 130303.3585],
                        [175130.0918, 130397.2857]
                    ]
                ]
            ]
        },
        "geometry_name": "geometry",
        "properties": {
            "id": 24636,
            "name": "test CA 24 05-10-2017",
            "organisationId": "xxxx-xxxx-xxx",
            "parentId": null,
            "reference": null,
            "siteStartDate": null,
            "siteEndDate": null,
            "Expr1": "xxxx-xxxx-xxxx-xxxx",
            "creationDate": "2018-06-05T11:26:41.673Z",
            "modificationDate": "2018-09-20T12:37:30.257Z",
            "siteId": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "type": "https://xxxxxx/cl/xxxxx/constructionZone",
            "validityEndDate": null,
            "Expr3": "2018-06-05T11:26:41.673Z",
            "nomBase": "ZZZ_Powalco_Test",
            "nomParent": null,
            "status": "Inscription",
            "idPowalco": "18007495",
            "bbox": [174877.4813, 130158.8387, 175205.9957, 130397.2857]
        }
    }, {
        "type": "Feature",
        "id": "Site_view.9e7bf413-ae67-4a40-9825-3c69ad4bdbfc",
        "geometry": null,
        "geometry_name": "geometry",
        "properties": {
            "id": 24638,
            "name": "test CA 24 05-10-2017",
            "organisationId": "xxxxxxxxx",
            "parentId": null,
            "reference": null,
            "siteStartDate": null,
            "siteEndDate": null,
            "Expr1": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "creationDate": "2018-06-05T11:26:45.116Z",
            "modificationDate": "2018-06-05T11:26:45.116Z",
            "siteId": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "type": "https://xxx/xx/SiteType/ZCCC",
            "validityEndDate": null,
            "Expr3": null,
            "nomBase": "ZZZ_Powalco_Test",
            "nomParent": null,
            "status": "Inscription",
            "idPowalco": "xxx"
        }
    }, {
        "type": "Feature",
        "id": "Site_view.xxxxxx",
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
                        [174865.8055, 130168.1263],
                        [175247.3917, 130213.768],
                        [175250.8413, 130192.5393],
                        [175109.405, 130153.7969],
                        [174866.867, 130142.9172],
                        [174865.8055, 130168.1263]
                    ]
                ],
                [
                    [
                        [174960.7581, 130303.3585],
                        [175130.0918, 130397.2857],
                        [175152.5814, 130316.5877],
                        [174960.7581, 130303.3585]
                    ]
                ]
            ]
        },
        "geometry_name": "geometry",
        "properties": {
            "id": 24637,
            "name": "test CA 24 05-10-2017",
            "organisationId": "3dd8aedd-cb82-4fa4-b932-2046ea7b7251",
            "parentId": null,
            "reference": null,
            "siteStartDate": null,
            "siteEndDate": null,
            "Expr1": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "creationDate": "2018-06-05T11:26:41.674Z",
            "modificationDate": "2018-09-20T12:37:30.257Z",
            "siteId": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "type": "https://xxxxxx/cl/SiteType/perimeterZone",
            "validityEndDate": null,
            "Expr3": "2018-06-05T11:26:41.674Z",
            "nomBase": "ZZZ_Powalco_Test",
            "nomParent": null,
            "status": "Inscription",
            "idPowalco": "18007495",
            "bbox": [174865.8055, 130142.9172, 175250.8413, 130397.2857]
        }
    }],
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:EPSG::31370"
        }
    },
    "bbox": [174865.8055, 130142.9172, 175250.8413, 130397.2857]
}

因此,我的要求是从数据中提取“类型”值。

"type":"https://xxxxxx/cl/SiteType/perimeterZone"
"type":"https://xxxxxx/cl/xxxxx/constructionZone"

我们可以不用正则表达式来做到吗?

仅供参考:我们正在使用OpenLayers3。

3 个答案:

答案 0 :(得分:2)

const types = data.features.map(feature => feature.properties.type);

答案 1 :(得分:1)

您可以做一件事:

  1. 解析结果。
  2. 获取所需的特定数据。

var jsonData = JSON.parse(result);

(jsonData.features).forEach(function(item) {
  console.log(item.properties.type);
});

答案 2 :(得分:0)

var data={
    "type": "FeatureCollection",
    "totalFeatures": 3,
    "features": [{
        "type": "Feature",
        "id": "Site_view.5b59c3a5-bc04-4707-93e7",
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
                        [174877.4813, 130159.3695],
                        [174991.5857, 130162.2884],
                        [175085.7881, 130172.9028],
                        [175204.6689, 130195.7236],
                        [175205.9957, 130192.274],
                        [175084.4613, 130170.7799],
                        [174989.7282, 130158.8387],
                        [174877.4813, 130159.3695]
                    ]
                ],
                [
                    [
                        [175130.0918, 130397.2857],
                        [175152.5814, 130316.5877],
                        [174960.7581, 130303.3585],
                        [175130.0918, 130397.2857]
                    ]
                ]
            ]
        },
        "geometry_name": "geometry",
        "properties": {
            "id": 24636,
            "name": "test CA 24 05-10-2017",
            "organisationId": "xxxx-xxxx-xxx",
            "parentId": null,
            "reference": null,
            "siteStartDate": null,
            "siteEndDate": null,
            "Expr1": "xxxx-xxxx-xxxx-xxxx",
            "creationDate": "2018-06-05T11:26:41.673Z",
            "modificationDate": "2018-09-20T12:37:30.257Z",
            "siteId": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "type": "https://xxxxxx/cl/xxxxx/constructionZone",
            "validityEndDate": null,
            "Expr3": "2018-06-05T11:26:41.673Z",
            "nomBase": "ZZZ_Powalco_Test",
            "nomParent": null,
            "status": "Inscription",
            "idPowalco": "18007495",
            "bbox": [174877.4813, 130158.8387, 175205.9957, 130397.2857]
        }
    }, {
        "type": "Feature",
        "id": "Site_view.9e7bf413-ae67-4a40-9825-3c69ad4bdbfc",
        "geometry": null,
        "geometry_name": "geometry",
        "properties": {
            "id": 24638,
            "name": "test CA 24 05-10-2017",
            "organisationId": "xxxxxxxxx",
            "parentId": null,
            "reference": null,
            "siteStartDate": null,
            "siteEndDate": null,
            "Expr1": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "creationDate": "2018-06-05T11:26:45.116Z",
            "modificationDate": "2018-06-05T11:26:45.116Z",
            "siteId": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "type": "https://xxx/xx/SiteType/ZCCC",
            "validityEndDate": null,
            "Expr3": null,
            "nomBase": "ZZZ_Powalco_Test",
            "nomParent": null,
            "status": "Inscription",
            "idPowalco": "xxx"
        }
    }, {
        "type": "Feature",
        "id": "Site_view.xxxxxx",
        "geometry": {
            "type": "MultiPolygon",
            "coordinates": [
                [
                    [
                        [174865.8055, 130168.1263],
                        [175247.3917, 130213.768],
                        [175250.8413, 130192.5393],
                        [175109.405, 130153.7969],
                        [174866.867, 130142.9172],
                        [174865.8055, 130168.1263]
                    ]
                ],
                [
                    [
                        [174960.7581, 130303.3585],
                        [175130.0918, 130397.2857],
                        [175152.5814, 130316.5877],
                        [174960.7581, 130303.3585]
                    ]
                ]
            ]
        },
        "geometry_name": "geometry",
        "properties": {
            "id": 24637,
            "name": "test CA 24 05-10-2017",
            "organisationId": "3dd8aedd-cb82-4fa4-b932-2046ea7b7251",
            "parentId": null,
            "reference": null,
            "siteStartDate": null,
            "siteEndDate": null,
            "Expr1": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "creationDate": "2018-06-05T11:26:41.674Z",
            "modificationDate": "2018-09-20T12:37:30.257Z",
            "siteId": "f3f0fe94-eec7-4ff0-8a12-5c6dfc67b38f",
            "type": "https://xxxxxx/cl/SiteType/perimeterZone",
            "validityEndDate": null,
            "Expr3": "2018-06-05T11:26:41.674Z",
            "nomBase": "ZZZ_Powalco_Test",
            "nomParent": null,
            "status": "Inscription",
            "idPowalco": "18007495",
            "bbox": [174865.8055, 130142.9172, 175250.8413, 130397.2857]
        }
    }],
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:EPSG::31370"
        }
    },
    "bbox": [174865.8055, 130142.9172, 175250.8413, 130397.2857]
};
data.features.map(feature => {
  console.log("Type : "+feature.properties.type);
});

只需使用地图迭代数据即可。