如何遍历 JSON

How to loop through JSON

我正在尝试遍历此 JSON 以获取 'name' 参数。数据来自微软BingAPI。我可以传入坐标来获取地名。我已经在下面粘贴了回复。以及我的尝试。请协助。

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

我认为您的问题很大一部分是您使用 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);
}

你可以试试下面的代码来解决你的问题:--

    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
      var text = '{ "data":{"rule":[{"clauses":{ "id":"q", "act":"the", "r":"1","cond":"2"}, "data":{"cond_oper":"7"}},{"clauses":{"id":"qw","act":"thefir","r":"1","cond":"1"},"data":{ "cond_oper":"7"}}]}}';
      var obj = JSON.parse(text);
      console.log(obj.data.rule.length);
      alert(obj.data.rule.length);

//Get the count 
// var count = obj.data.rule.length

      for(var i=0; i<obj.data.rule.length; i++)
      {
         alert(obj.data.rule[i].clauses.id);
         console.log(obj.data.rule[i].clauses.id)
      }
    </script>
</head>
<body>

</body>
</html>

您的问题是 var i;i 未初始化,仅声明。使用示例 lat/long.

使用 = 0 初始化 i 这对我来说在本地工作正常
$.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]);
    }
  });
});