如何循环JSON

时间:2018-03-08 04:43:19

标签: javascript json for-loop bing-api

我正在尝试遍历此JSON以获取'name'参数。数据来自微软的Bing API。我可以通过坐标来获取地点的名称。我已粘贴下面的回复。以及我的尝试。请协助。

{  
   "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":"Location:http://schemas.microsoft.com/search/local/ws/rest/v1",
               "bbox":[  
                  47.636677282429325,
                  -122.13698331308882,
                  47.64440271757068,
                  -122.12169668691118
               ],
               "name":"1 Microsoft Way, Redmond, WA 98052",
               "point":{  
                  "type":"Point",
                  "coordinates":[  
                     47.64054,
                     -122.12934
                  ]
               },
               "address":{  
                  "addressLine":"1 Microsoft Way",
                  "adminDistrict":"WA",
                  "adminDistrict2":"King Co.",
                  "countryRegion":"United States",
                  "formattedAddress":"1 Microsoft Way, Redmond, WA 98052",
                  "locality":"Redmond",
                  "postalCode":"98052"
               },
               "confidence":"Medium",
               "entityType":"Address",
               "geocodePoints":[  
                  {  
                     "type":"Point",
                     "coordinates":[  
                        47.64054,
                        -122.12934
                     ],
                     "calculationMethod":"Interpolation",
                     "usageTypes":[  
                        "Display",
                        "Route"
                     ]
                  }
               ],
               "matchCodes":[  
                  "Good"
               ]
            }
         ]
      }
   ],
   "statusCode":200,
   "statusDescription":"OK",
   "traceId":"089a91ac5b694010884d6a7b7d245718|CH12F221B8|7.7.0.0|CH1AAPBD7C89012"
}

我尝试过以下操作,但得到一个未定义的长度错误:

this.http.get('http://dev.virtualearth.net/REST/v1/Locations/'+this.latitude+','+this.longitide+'?o=json&key=AgThwaQToIr5UwjAisaBegjG3qpxBfgFL354mlTxiRPGOrqId8nShnugy40jpebW').subscribe(data => {
  this.place = data;
  for(var i; i < this.place.resourceSets.length; i++){
    this.dataset = this.place.resourceSets[i].resources;
    console.log(this.dataset);
  }
  }) 
}

3 个答案:

答案 0 :(得分:1)

我认为您问题的一个重要部分是您使用this进行本地变量分配。理想情况下,您应该使用let,但对于向后兼容的浏览器,您始终可以使用var

见下文,特别是循环,执行var dataset并将长度缓存到变量n

var place = {
  "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": "Location:http://schemas.microsoft.com/search/local/ws/rest/v1",
      "bbox": [
        47.636677282429325, -122.13698331308882,
        47.64440271757068, -122.12169668691118
      ],
      "name": "1 Microsoft Way, Redmond, WA 98052",
      "point": {
        "type": "Point",
        "coordinates": [
          47.64054, -122.12934
        ]
      },
      "address": {
        "addressLine": "1 Microsoft Way",
        "adminDistrict": "WA",
        "adminDistrict2": "King Co.",
        "countryRegion": "United States",
        "formattedAddress": "1 Microsoft Way, Redmond, WA 98052",
        "locality": "Redmond",
        "postalCode": "98052"
      },
      "confidence": "Medium",
      "entityType": "Address",
      "geocodePoints": [{
        "type": "Point",
        "coordinates": [
          47.64054, -122.12934
        ],
        "calculationMethod": "Interpolation",
        "usageTypes": [
          "Display",
          "Route"
        ]
      }],
      "matchCodes": [
        "Good"
      ]
    }]
  }],
  "statusCode": 200,
  "statusDescription": "OK",
  "traceId": "089a91ac5b694010884d6a7b7d245718|CH12F221B8|7.7.0.0|CH1AAPBD7C89012"
}


for (var i=0,n=place.resourceSets.length; i<n; i++) {
  var dataset = place.resourceSets[i].resources;
  console.log(dataset);
}

答案 1 :(得分:0)

您可以针对您的问题尝试以下代码: -

<?php

...

class BaseController extends Controller
{
    public function theme_options() {
        // Set number of columns
        $footer_cols = DB::table('theme_settings')->where('id','=','200')->value('setting_value');
        $widget_width = 12 / $footer_cols;

        // Footer Settings
        $data = array(
            'widget_width' => $widget_width
        );

        return $data;
    }
}

答案 2 :(得分:0)

您的问题是var i;i未初始化仅声明。使用i初始化= 0这对我来说在使用示例lat / long时可以正常工作。

$.get('http://dev.virtualearth.net/REST/v1/Locations/47.640568390488625,-122.1293731033802?o=json&key=AgThwaQToIr5UwjAisaBegjG3qpxBfgFL354mlTxiRPGOrqId8nShnugy40jpebW',function(data){
    this.place = data;
    console.log(this.place.resourceSets.length);
    for(var i = 0; i < this.place.resourceSets.length; i++){
      console.log(this.place.resourceSets[0]);
    }
  });
});