【问题标题】:getting spefic values from JSON data从 JSON 数据中获取特定值
【发布时间】:2019-03-30 23:04:58
【问题描述】:

我从 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。

【问题讨论】:

  • 你尝试了什么?我认为你不应该用正则表达式来做。
  • 不使用正则表达式可以吗?是的,你可以。
  • 在寻求帮助之前,您至少应该尝试自己解决问题。
  • @SubhranilSengupta 基本上是 vlaz 所说的。当您在堆栈溢出上发帖时,您应该说出您尝试过的内容,而您的问题不会涉及您尝试过的任何内容。查看您尝试过的方法并了解它为什么不起作用有助于我们回答您的问题(所以我们不会告诉您去做您已经尝试过的事情,也不会帮助说明您要寻求什么样的解决方案) .
  • @RockySims 谢谢,我尝试了正则表达式和其他一些方法来获取值,但失败了。下次我会好好照顾的。

标签: javascript json regex ajax openlayers-3


【解决方案1】:
const types = data.features.map(feature => feature.properties.type);

【讨论】:

    【解决方案2】:

    你可以做一件事:

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

    var jsonData = JSON.parse(result);
    
    (jsonData.features).forEach(function(item) {
      console.log(item.properties.type);
    });

    【讨论】:

      【解决方案3】:

      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);
      });

      只需使用地图迭代您的数据。

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多