arrays - 如何访问这个多维 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"
}
]
}
]
}
解决方案
这只是阅读 JSON 和访问数组的问题。
所以这对我有用
data.features[0].geometry.coordinates;
还有这个
data["features"][0]["geometry"]['coordinates'];
推荐阅读
- angular - 手动销毁组件
- java-9 - Java 9 - 列表类:of() 带有可变参数的重载方法
- google-cloud-platform - 谷歌云平台无权SSH下载
- java - Java中的三元组是什么?
- tfs - TFS 服务器丢失,而后端 SQL Server 完好无损。如何将源代码迁移到 VSTS?
- linux - 在所有 bash 命令后自动运行命令?
- idris - 如何在 Idris 中编写一个简单的基于列表的快速排序?
- javascript - 允许 SPA 客户端使用客户端凭据授予令牌和个人访问令牌 - Laravel Passport
- ruby - 如何使用 Facebook Business SDK 检索页面的多个字段
- r - R:使用 lapply 同时在两个列表上迭代一个函数?