【问题标题】:jQuery Each Multidimensional JSON ArrayjQuery 每个多维 JSON 数组
【发布时间】:2017-01-18 07:59:14
【问题描述】:

我有以下问题,我现在尝试了 3 天都无法解决。我有一个 JSON 数组:

var geojson = {
      type: 'FeatureCollection',
      features: [
        {
          "type": "Feature",
          "geometry": {
            "coordinates": [
              5.140439,
              51.686608
            ],
            "type": "Point"
          },
          "properties": {
            "title": "TEST1",
            "rentals": true,
            "tackleshop": false,
            "fuel": false,
            "marker-color": "#1087bf",
            "marker-size": "large",
            "marker-symbol": "harbor"
          }
        },
        {
          "type": "Feature",
          "geometry": {
            "coordinates": [
              5.134060,
              51.686890
            ],
            "type": "Point"
          },
          "properties": {
            "title": "TEST2",
            "rentals": true,
            "tackleshop": false,
            "fuel": true,
            "marker-color": "#1087bf",
            "marker-size": "large",
            "marker-symbol": "harbor"
          }
        },
        {
          "type": "Feature",
          "geometry": {
            "coordinates": [
              5.133729,
              51.681425
            ],
            "type": "Point"
          },
          "properties": {
            "title": "TEST3",
            "rentals": false,
            "tackleshop": true,
            "fuel": true,
            "marker-color": "#1087bf",
            "marker-size": "large",
            "marker-symbol": "harbor"
          }
        }
      ]
    };

我现在测试的内容:

$.each(geojson, function() {
            $.each(this, function(key, value) {
              $.each(this, function(value, featuress) {
              console.log(featuress.properties.title);

              });
            });
        });

我想要的结果: 我想要一个通过这个 json 数组的 $.each,我可以在其中显示变量,例如:每个功能的“标题”。

谁能帮帮我?干杯!

【问题讨论】:

    标签: json multidimensional-array


    【解决方案1】:

    您在 JSON 对象上循环了 2 次,我删除了外部的 foreach 循环。在这种情况下,您只能遍历列表 - features。看sn-p:

    var geojson = {  
       "type":"FeatureCollection",
       "features":[  
          {  
             "type":"Feature",
             "geometry":{  
                "coordinates":[  
                   5.140439,
                   51.686608
                ],
                "type":"Point"
             },
             "properties":{  
                "title":"TEST1",
                "rentals":true,
                "tackleshop":false,
                "fuel":false,
                "marker-color":"#1087bf",
                "marker-size":"large",
                "marker-symbol":"harbor"
             }
          }
       ]
    }
    
    $.each(geojson.features, function(i, feature) {
      console.log(feature.properties.title);
    });
    
    You looped over an object
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    进一步阅读:

    【讨论】:

      【解决方案2】:

      作为您的代码,geojson 是一个 json 对象,它不是一个 json 数组。

      json 数组的示例是 [{"name":"item 1"},{"name": "item2} ]

      要查看列表功能并显示其属性,您可以 试试这个:

      $.each(geojson.features, function(index, feature) {
          console.log(feature.properties.title);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-01
        • 2013-10-22
        相关资源
        最近更新 更多