首页 > 解决方案 > 如何访问这个多维 json 数组中的数据?

问题描述

出于某种原因,我很难访问由 api 调用返回的多维数组。我花了几个小时尝试不同的结果,但没有成功。我想我需要第二双眼睛。我正在尝试访问地址的坐标、纬度和经度。我试过了data.features.geometry.coordinates,我得到了

错误:几何未定义

Ajax API 调用:

            $.ajax({
                url: 'https://api.mapbox.com/access_token',
                dataType: 'json',
                type: 'GET',
                contentType:'json',
                success: function(data)
                {

                console.log(data);

                }
            });

控制台返回这个(对不起大数据):

{
    "type": "FeatureCollection",
    "features": [
        {
            "id": "address.2829218120531924",
            "type": "Feature",
            "place_type": [
                "address"
            ],
            "relevance": 1,
            "properties": {
                "accuracy": "rooftop"
            },
            "text": "Southwest 59th Court",
            "place_name": "7313 Southwest 59th Court, Miami, Florida 33143, United States",
            "matching_place_name": "7313 Southwest 59th Court, Miami, Florida 33143, United States of America",
            "center": [
                -80.290092,
                25.70304
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -80.290092,
                    25.70304
                ]
            },
            "address": "7313",
            "context": [
                {
                    "id": "neighborhood.277794",
                    "text": "South Miami"
                },
                {
                    "id": "postcode.9047361780999760",
                    "text": "33143"
                },
                {
                    "id": "place.9791754866122320",
                    "wikidata": "Q8652",
                    "text": "Miami"
                },
                {
                    "id": "region.12218597351235010",
                    "short_code": "US-FL",
                    "wikidata": "Q812",
                    "text": "Florida"
                },
                {
                    "id": "country.9053006287256050",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        },
        {
            "id": "address.4735143261475820",
            "type": "Feature",
            "place_type": [
                "address"
            ],
            "relevance": 0.836667,
            "properties": {
                "accuracy": "street"
            },
            "text": "Southwest 59th Court",
            "place_name": "Southwest 59th Court, Miami, Florida 33143, United States",
            "matching_place_name": "Southwest 59th Court, Miami, Florida 33143, United States of America",
            "center": [
                -80.2906123,
                25.7175916
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -80.2906123,
                    25.7175916
                ]
            },
            "context": [
                {
                    "id": "neighborhood.277794",
                    "text": "South Miami"
                },
                {
                    "id": "postcode.9047361780999760",
                    "text": "33143"
                },
                {
                    "id": "place.9791754866122320",
                    "wikidata": "Q8652",
                    "text": "Miami"
                },
                {
                    "id": "region.12218597351235010",
                    "short_code": "US-FL",
                    "wikidata": "Q812",
                    "text": "Florida"
                },
                {
                    "id": "country.9053006287256050",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        },
        {
            "id": "address.3831267361566610",
            "type": "Feature",
            "place_type": [
                "address"
            ],
            "relevance": 0.801333,
            "properties": {
                "accuracy": "interpolated"
            },
            "text": "Southwest 59th Avenue",
            "place_name": "7313 Southwest 59th Avenue, Miami, Florida 33143, United States",
            "matching_place_name": "7313 Southwest 59th Avenue, Miami, Florida 33143, United States of America",
            "center": [
                -80.289343,
                25.704199
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -80.289343,
                    25.704199
                ],
                "interpolated": true
            },
            "address": "7313",
            "context": [
                {
                    "id": "neighborhood.277794",
                    "text": "South Miami"
                },
                {
                    "id": "postcode.9047361780999760",
                    "text": "33143"
                },
                {
                    "id": "place.9791754866122320",
                    "wikidata": "Q8652",
                    "text": "Miami"
                },
                {
                    "id": "region.12218597351235010",
                    "short_code": "US-FL",
                    "wikidata": "Q812",
                    "text": "Florida"
                },
                {
                    "id": "country.9053006287256050",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        },
        {
            "id": "postcode.9047361780999760",
            "type": "Feature",
            "place_type": [
                "postcode"
            ],
            "relevance": 0.787778,
            "properties": {},
            "text": "33143",
            "place_name": "Miami, Florida 33143, United States",
            "matching_place_name": "Miami, Florida 33143, United States of America",
            "bbox": [
                -80.3349178558439,
                25.6880751353229,
                -80.2450230532034,
                25.7188419508551
            ],
            "center": [
                -80.29,
                25.71
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -80.29,
                    25.71
                ]
            },
            "context": [
                {
                    "id": "place.9791754866122320",
                    "wikidata": "Q8652",
                    "text": "Miami"
                },
                {
                    "id": "region.12218597351235010",
                    "short_code": "US-FL",
                    "wikidata": "Q812",
                    "text": "Florida"
                },
                {
                    "id": "country.9053006287256050",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        },
        {
            "id": "address.7544455348675994",
            "type": "Feature",
            "place_type": [
                "address"
            ],
            "relevance": 0.755,
            "properties": {
                "accuracy": "street"
            },
            "text": "Southwest 59th Court",
            "place_name": "Southwest 59th Court, Miami, Florida 33156, United States",
            "matching_place_name": "Southwest 59th Court, Miami, Florida 33156, United States of America",
            "center": [
                -80.2890202,
                25.6464222
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -80.2890202,
                    25.6464222
                ]
            },
            "context": [
                {
                    "id": "neighborhood.270046",
                    "text": "Deering Bay"
                },
                {
                    "id": "postcode.9496629846896750",
                    "text": "33156"
                },
                {
                    "id": "place.9791754866122320",
                    "wikidata": "Q8652",
                    "text": "Miami"
                },
                {
                    "id": "region.12218597351235010",
                    "short_code": "US-FL",
                    "wikidata": "Q812",
                    "text": "Florida"
                },
                {
                    "id": "country.9053006287256050",
                    "short_code": "us",
                    "wikidata": "Q30",
                    "text": "United States"
                }
            ]
        }
    ]
}

标签: arraysjsonmultidimensional-array

解决方案


这只是阅读 JSON 和访问数组的问题。

所以这对我有用

data.features[0].geometry.coordinates;

还有这个

data["features"][0]["geometry"]['coordinates'];

推荐阅读