Ad

Getting Spefic Values From JSON Data

- 1 answer

I am getting the data from ajax call in javascript:

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

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

We are getting the response in this manner:

{
    "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]
}

So here my requirement is to extract the "type" values from the data.

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

Can we do it without using Regex.

FYI: We are using OpenLayers3.

Ad

Answer

You can do one thing:

  1. Parse the result.
  2. Get specific data that you want.

var jsonData = JSON.parse(result);

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

Ad
source: stackoverflow.com
Ad