JSON AngularJS多个对象

JSON AngularJS multiple objects

本文关键字:对象 AngularJS JSON      更新时间:2023-09-26

我只是好奇如何访问这个JSON调用中的任何字段,因为它有多个对象,非常令人困惑,因为这是我第一次处理这个问题。以下是指向JSON的链接:http://api.wunderground.com/api/5ad0204df4bdbeff/forecast/q/Australia/Sydney.json?callback=JSON_CALLBACK

我的代码使用AngularJS调用JSON并存储它。然后绑定值通过循环访问它。

<!DOCTYPE html>
<html ng-app="plunker">
  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.25/angular.js" data-semver="1.2.25"></script>
    <script >
    var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $http) {
   $http.jsonp('http://api.wunderground.com/api/5ad0204df4bdbeff/forecast/q/Australia/Sydney.json?callback=JSON_CALLBACK').success(function(data){
     console.log(data);
     $scope.weatherData=data;
  })
});
    </script>
  </head>
  <body ng-controller="MainCtrl">
   <ul>
        <li ng-repeat="weather in weatherData">
            {{weather.celsius}}
        </li>
    </ul>   
  </body>
</html>

本质上,我想访问"simpleforecast"对象,然后访问温度区域。我该怎么做?

编辑:我为您制作了一把小提琴来演示ng repeat的用法。这是FIDDLE

它一定是这样的:

<ul>
    <li ng-repeat="forecast in weatherData.forecast.simpleforecast.forecastday">
        <br/>date: {{forecast.date.pretty}} 
        <br/>high: {{forecast.high.celsius}},  
        <br/>low: {{forecast.low.celsius}}
    </li>
</ul>

我建议使用一些JSON查看器/格式化程序工具来从复杂的JSON中找到您想要的数据。事实上,我是用这个工具找到的。

获得今天的低温和高温

data.forecast.simpleforecast.forecastday[0].high.celsius
data.forecast.simpleforecast.forecastday[0].low.celsius

此外,该服务还提供4天的天气预报,如果您想让他们更改天气预报日的指数。0是今天,1是明天,依此类推。

<li ng-repeat="weather in weatherData.forecast.forecast.simpleforecast">
   {{weather.forecastday[0].high.celsius}}
   {{weather.forecastday[0].low.celsius}}
</li>