Search code examples
javascriptjsonfor-loopbing-api

How to loop through JSON


I am trying to loop through this JSON in order to get to the 'name' parameter. The data comes from Microsoft's Bing API. I can pass in coordinates to get the name of a place. I have pasted the response below. as well as my attempt. Please assist.

{  
   "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"
}

I have tried the following but am getting a length undefined error:

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);
  }
  }) 
}

Solution

  • I think a big part of your problem is you're using this for your local variable assignments. Ideally you should use let but for backwards compatible browsers you can always use var.

    See below, especially the loop, which performs var dataset and also caches the length to the variable 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);
    }