2017-05-21 112 views
0
數組

你好,我有個城市的數組創建多個JSON文件

var cityArr = ["London", "Beijing", "Paris", "New York", "Seoul", "HongKong"]; 

我想通過每一個城市進行迭代,並使用AngularJS得到一個JSON API和Javascript,這裏是我的代碼:

for (i=0; i<cityArr.length; i++){ 

    $scope.$watch('fondCity', function() { 
     cityService.city = $scope.foundCity; 
    }); 
    var newUrl = "http://api.waqi.info/feed/" + cityArr[i] + "/?token=5336bd836b9ec064f6c6fe0bf7e2781838c15c87"; 
     $http({ 
      method: "GET", 
      url: newUrl 
     }).then(function mySucces(response) { 
      $scope.newData = response.data; 




     }, function myError(response) { 
      $scope.newData = response.statusText; 
     }); 
    } 

如何將每個JSON文件添加到單個陣列。

JSON單個文件

{ 
    "status": "ok", 
    "data": { 
    "aqi": 49, 
    "idx": 5724, 
    "attributions": [ 
     { 
     "url": "http://uk-air.defra.gov.uk/", 
     "name": "UK-AIR, air quality information resource - Defra, UK" 
     }, 
     { 
     "url": "http://londonair.org.uk/", 
     "name": "London Air Quality Network - Environmental Research Group, King's College London" 
     } 
    ], 
    "city": { 
     "geo": [ 
     51.5073509, 
     -0.1277583 
     ], 
     "name": "London", 
     "url": "http://aqicn.org/city/london/" 
    } 
} 

我希望它看起來像這樣的:

{ 
    "status": "ok", 
    "data": { 
    "aqi": 49, 
    "idx": 5724, 
    "attributions": [ 
     { 
     "url": "http://uk-air.defra.gov.uk/", 
     "name": "UK-AIR, air quality information resource - Defra, UK" 
     }, 
     { 
     "url": "http://londonair.org.uk/", 
     "name": "London Air Quality Network - Environmental Research Group, King's College London" 
     } 
    "status": "ok", 
    "data": { 
    "aqi": 155, 
    "idx": 1451, 
    "attributions": [ 
     { 
     "url": "http://www.bjmemc.com.cn/", 
     "name": "Beijing Environmental Protection Monitoring Center (北京市環境保護監測中心)" 
     }, 
     { 
     "url": "http://beijing.usembassy-china.org.cn/070109air.html", 
     "name": "U.S Embassy Beijing Air Quality Monitor (美國駐北京大使館空氣質量監測)" 
     } 
    ], 
{ 
    "status": "ok", 
    "data": { 
    "aqi": 28, 
    "idx": 5722, 
    "attributions": [ 
     { 
     "url": "http://www.airparif.asso.fr/", 
     "name": "AirParif - Association de surveillance de la qualité de l'air en Île-de-France" 
     } 
    ], 

回答

1

由於每個響應共享相同的屬性,所以您可以實現的最好方法是獲得每個響應的數組。

通過使用$q,您可以將$httpmap的承諾一起使用。

var promises = cityArr.map(function(name) { 
    return $http.get("http://api.waqi.info/feed/" + name + "/?token=5336bd836b9ec064f6c6fe0bf7e2781838c15c87"); 
}); // For each City Name, create an array filled with Promises. 

// Wait for all the Promises to be completed. 
$q.all(promises).then(function(data) { 
    // Create an array of the data attribute from each response. 
    var results = data.map(function(result) { 
     return result.data; 
    }); 

    console.log(results); 
}); 

您將需要添加$q作爲依賴於你的控制器或組件。

我創建了一個JSBin例如:http://jsbin.com/gukusot/edit?js,console

-1

重複的在JSONs

{ 
    "status": "ok", 
    "status": "not ok" 
}

關鍵的定義不應該使用。

您應該將每個JSON回覆包裝到一個對象中,並且可以將這些對象連接成一個數組。

 

    var myApp = angular.module('myApp',[]); 

    function MyCtrl($scope, $http) { 
    var cityArr = ["London", "Beijing", "Paris", "New York", "Seoul", "HongKong"]; 
    $scope.newData = {}; 

    angular.forEach(cityArr, function(value) { 
    var newUrl = "http://api.waqi.info/feed/" + value + "/?token=5336bd836b9ec064f6c6fe0bf7e2781838c15c87"; 
     $http({ 
      method: "GET", 
      url: newUrl 
     }).then(function mySucces(response) { 
      $scope.newData[value] = response.data; 
      console.log($scope.newData); 
     }, function myError(response) { 
      $scope.newData[value] = response.statusText; 
     }); 
    }); 
} 

期望的結果:

 

    [ 
    "London": {"status": "ok", ...}, 
    "Beijing": {"status": ... , ... }, 
    ... 
    ] 

注:

  1. 不污染在全球範圍內,使用for循環變量VAR關鍵字。我更喜歡內置函數來遍歷你的數組,例如, angular.forEach。
+0

這其實不是一個問題的答案,但評論代替。在這種情況下請嘗試評論。 –