Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问JSON中的变量?_Javascript_Angular - Fatal编程技术网

Javascript 访问JSON中的变量?

Javascript 访问JSON中的变量?,javascript,angular,Javascript,Angular,我目前正在使用Bing等时线Api。我使用HTTPClient在Angular中设置http请求。这是我得到的数据集的示例: { "authenticationResultCode": "ValidCredentials", "brandLogoUri": "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png", "copyright": "Copyright © 2018 Microsoft and its supplier

我目前正在使用Bing等时线Api。我使用HTTPClient在Angular中设置http请求。这是我得到的数据集的示例:

{
"authenticationResultCode": "ValidCredentials",
"brandLogoUri": "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
"copyright": "Copyright © 2018 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
"resourceSets": [{
    "estimatedTotal": 1,
    "resources": [{
        "__type": "IsochroneResponse:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
        "origin": {
            "latitude": 47.640068,
            "longitude": -122.129858
        },
        "polygons": [{
            "coordinates": [
                [
                    [48.22848, -122.12867],
                    [48.22613, -122.10625],
                    [48.229309, -122.08228],
                    [48.23733, -122.07666],
                    [48.24474, -122.05325],
                    [48.24469, -122.0532],
                    [48.24424, -122.05386],
                    [48.23119, -122.06654],         
                    [48.22848, -122.12867]
                ]
            ]
        }]
    }]
}],
"statusCode": 200,
"statusDescription": "OK",
"traceId": "4ed97517798141a1b5bb9df40509f190|CO30305304|7.7.0.0|"
}

我可以用这个找到资源集

 this
        .http
        .get(`http://dev.virtualearth.net/REST/v1/Routes/Isochrones?waypoint=\
        ${this.testPointlat},${this.testPointlong}&maxTime=15&timeUnit=Minutes\
        &dateTime=2017-11-27T18:00:00-08:00&travelMode=Driving\
        &key=$$$$$$$$$$$$$
        `).subscribe(
          (response) => {
            this.driveTimeCoords = response.resourceSets;
            console.log(this.driveTimeCoords);
            const polygons = this.driveTimeCoords.resources.polygons;
            console.log(polygons);
          }
        );
      })
所以这个.driveTimeCoords给了我一个数组。。。我的尝试后,它显然没有工作,因为它说未定义。我会用if或别的什么来做一个foreach吗?我可能想得太多了。我想要的只是坐标,这样我就可以了。将它们映射到一个geojson特性组中,用于传单

谢谢

编辑: 在console.log this.driveTimeCoords上我得到

[{…}]0: estimatedTotal: 1resources: [{…}]

JSON格式设置已关闭:请注意资源集、资源和多边形是如何作为对象数组的,这意味着您需要调用数组的索引来访问数据,如下所示:

this.driveTimeCoords = response.resourceSets[0];
console.log(this.driveTimeCoords);
const polygons = this.driveTimeCoords.resources[0].polygons[0];
console.log(polygons);
要解决此问题,JSON的格式应如下所示:

{
"authenticationResultCode": "ValidCredentials",
"brandLogoUri": "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
"copyright": "Copyright © 2018 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
"resourceSets": {
    "estimatedTotal": 1,
    "resources": {
        "__type": "IsochroneResponse:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
        "origin": {
            "latitude": 47.640068,
            "longitude": -122.129858
        },
        "polygons": {
            "coordinates": [
                {"latitude": 48.22848, "longitude": -122.12867},
                {"latitude": 48.22613, "longitude": -122.10625},
                {"latitude": 48.229309, "longitude": -122.08228},
                {"latitude": 48.23733, "longitude": -122.07666},
                {"latitude": 48.24474, "longitude": -122.05325},
                {"latitude": 48.24469, "longitude": -122.0532},
                {"latitude": 48.24424, "longitude": -122.05386},
                {"latitude": 48.23119, "longitude": -122.06654},         
                {"latitude": 48.22848, "longitude": -122.12867}
            ]
         }
    }
},
"statusCode": 200,
"statusDescription": "OK",
"traceId": "4ed97517798141a1b5bb9df40509f190|CO30305304|7.7.0.0|"
}

我在坐标中添加了变量名,以便更容易理解正在读取的数据。

根据JSON对象判断,它看起来是这样的。driveTimeCoords是对JSON中资源集的引用,是一个数组,对于您似乎想要资源的每个属性,多边形、坐标都是数组。因此,必须执行一系列嵌套的.map操作

请尝试以下操作:

var result = this.driveTimeCoords.map((obj)=>{
    return obj.resources.map((resource)=>{
       return resource.polygon.map((poly)=> poly.coordinates )
    })
})

这样做,使得如果这些数组中的任何一个包含多个引用,您都可以得到它们。之后,您可以展平数组,或者简单地将第一个引用为另一个人建议的结果[0][0][0]

由于它们是数组,您需要通过其索引访问:

让json={ authenticationResultCode:ValidCredentials, brandLogoUri:http:\/\/dev.virtualearth.net\/Branding\/logo\u powered\u by.png, 版权:版权所有©2018 Microsoft及其供应商。保留所有权利。未经Microsoft Corporation明确书面许可,不得访问本API,不得以任何方式使用、复制或传播本API的内容和任何结果。, 资源集:[{ 估计总数:1, 资源:[{ __类型:isochronereresponse:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1, 来源:{ 纬度:47.640068, 经度:-122.129858 }, 多边形:[{ 坐标:[ [ [48.22848, -122.12867], [48.22613, -122.10625], [48.229309, -122.08228], [48.23733, -122.07666], [48.24474, -122.05325], [48.24469, -122.0532], [48.24424, -122.05386], [48.23119, -122.06654], [48.22848, -122.12867] ] ] }] }] }], 状态代码:200, 状态描述:好的, traceId:4ed97517798141a1b5bb9df40509f190 | CO30305304 | 7.7.0.0| } 让polys=json['resourceSets'][0]。资源[0]。多边形; 设o=document.getElementById'output'; o、 innerHTML=JSON.stringifypolys;
通过此格式设置,您可以使用以下路径协调访问:

.resourceSets[0].resources[0].polygons[0].coordinates[0][0][0] = 48.22848
.resourceSets[0].resources[0].polygons[0].coordinates[0][0][1] = -122.12867
.resourceSets[0].resources[0].polygons[0].coordinates[0][1][0] = 48.22613
.resourceSets[0].resources[0].polygons[0].coordinates[0][1][1] = -122.10625
.resourceSets[0].resources[0].polygons[0].coordinates[0][2][0] = 48.229309
.resourceSets[0].resources[0].polygons[0].coordinates[0][2][1] = -122.08228
.resourceSets[0].resources[0].polygons[0].coordinates[0][3][0] = 48.23733
.resourceSets[0].resources[0].polygons[0].coordinates[0][3][1] = -122.07666
.resourceSets[0].resources[0].polygons[0].coordinates[0][4][0] = 48.24474
.resourceSets[0].resources[0].polygons[0].coordinates[0][4][1] = -122.05325
.resourceSets[0].resources[0].polygons[0].coordinates[0][5][0] = 48.24469
.resourceSets[0].resources[0].polygons[0].coordinates[0][5][1] = -122.0532
.resourceSets[0].resources[0].polygons[0].coordinates[0][6][0] = 48.24424
.resourceSets[0].resources[0].polygons[0].coordinates[0][6][1] = -122.05386
.resourceSets[0].resources[0].polygons[0].coordinates[0][7][0] = 48.23119
.resourceSets[0].resources[0].polygons[0].coordinates[0][7][1] = -122.06654
.resourceSets[0].resources[0].polygons[0].coordinates[0][8][0] = 48.22848
.resourceSets[0].resources[0].polygons[0].coordinates[0][8][1] = -122.12867

console.logthis.driveTimeCoords;给你一个输出?您介意发布itIt吗?它给我一个[{}],其中包含估计的总资源,itIt是一个数组,因此您需要引用数组中的索引。所以你可以在它上面循环或者直接引用索引。你只是想在resourceset.resources.polygons中的坐标数组中添加类似lat/lng的键,这样你就可以更容易地映射一个ILatLng模型还是什么的?根据你所说的和我所看到的,我不确定我是否理解你想要什么。Chris W我只想删除坐标数组并将其分配给另一个变量。