2015-09-11 114 views
2

我想解析一个JSON文件,我为我正在创建的应用程序创建。 JSON文件看起来是这样的:用AngularJS解析JSON文件

{"thresholds":[ 
    {"heatIndex":[ 
     {"maxTemp":[{"red":"125","orange":"105","yellow":"90","green":"-80"}]}, 
     {"minTemp":[{"red":"-50","orange":"-35","yellow":"-20","green":"0"}]} 
    ]}, 
    {"wind":[ 
     {"speed":[{"red":"34.8","orange":"26.1","yellow":"17.4","green":"0"}]}, 
     {"gust":[{"red":"50.4","orange":"39.1","yellow":"26.1","green":"0"}]} 
    ]} 
]} 

我需要让我的角度应用输出的数据对我和一直在试图遵循http.get教程,我不能让我的数据出来。基本的一部分,我需要怎么我的角度应用程序的该部分格式的帮助:在您的$ HTTP请求

<ul> 
    <li ng-repeat="x in thresholds"> 
     {{x.thresholds}} 
    </li> 
    </ul> 
    </div> 

    <!-- NG APP Script --> 
    <script> 
    var ehwo2App = angular.module('ehwo2App' , []); 
    ehwo2App.controller('ehwo2Ctrl' , function ($scope, $http) { 
     $http.get('config.json').success(function(data) { 
      $scope.thresholds = data; 
     }); 
     }); 
    </script> 
+0

$ scope.thresholds = JSON.parse(data); (因为数据是即将到来的字符串我想)问你的朋友谷歌之前发布问题http://stackoverflow.com/questions/4935632/parse-json-in-javascript –

+2

@DmitriAlgazin实际上你不必这样做,角度自动为你解析json。除非你知道你是对的,否则试着在评论时不要低估。 –

回答

1

,尝试对其进行更改:

$scope.thresholds = data; 

$scope.thresholds = data.thresholds; 
+0

感谢兄弟。尽管如此,仍然不确定要进一步钻取。例如,如果我想maxTemp:红色值等... –

+0

我也有一个问题,记住这一点。我相信你应该可以像访问JavaScript对象一样访问数据。所以最高临时工将是$ scope.thresholds.heatIndex.maxTemp检查了这一点https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – Nibb

0

首先请注意,success快捷方式现在已被弃用,您应该使用标准.then方法(请参阅this page上的红色方框。

其次,你还没有说实话什么确切地不起作用。尝试使用下面的代码,并让我们知道在控制台中记录的内容。

ehwo2App.controller('ehwo2Ctrl' , function ($scope, $http) { 

    console.log("Making GET request"); 

    $http.get('http://www.dev.nids.noaa.gov/~mneedham/config.json') 

    .then(function(response) { 
    $scope.thresholds = response.data.thresholds; 
    console.log($scope.thresholds); 
    }) 

    .catch(function(error){ 
    console.error("Error with GET request", e); 
    }); 

});