简体   繁体   English

JSON AngularJS多个对象

[英]JSON AngularJS multiple objects

I'm just curious as in how to access any field in this JSON call as it has multiple objects and is very confusing as it is my first time dealing with this. 我只是好奇如何访问此JSON调用中的任何字段,因为它具有多个对象,并且非常令人困惑,因为这是我第一次处理此字段。 Here is the link to the JSON: http://api.wunderground.com/api/5ad0204df4bdbeff/forecast/q/Australia/Sydney.json?callback=JSON_CALLBACK 以下是指向JSON的链接: http : //api.wunderground.com/api/5ad0204df4bdbeff/forecast/q/Australia/Sydney.json? callback= JSON_CALLBACK

My code uses AngularJS to call the JSON and then store it. 我的代码使用AngularJS调用JSON,然后将其存储。 A bound value then access it by looping through. 绑定值然后通过循环访问它。

<!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>

Essentially I want to access the "simpleforecast" object and then access the temperature area. 本质上,我想访问“ simpleforecast”对象,然后访问温度区域。 How would I go about this? 我将如何处理?

EDIT: I made a fiddle for you to demonstrate the usage with ng-repeat. 编辑:我为您做了一个小提琴,以演示ng-repeat的用法。 Here is FIDDLE 这是FIDDLE

It must be like that: 一定是这样的:

<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>

I recommend to use some JSON viewer/formatter tool to find the data you want from a complex JSON. 我建议使用一些JSON查看器/格式化程序工具从复杂的JSON中查找所需的数据。 Actually I found it using that tool. 实际上,我是使用工具找到它的。

To get today's low and high temperatures 为了获得今天的低温和高温

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

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

Also service gives 4 days of weather, if you want them change index of forecast day. 如果您希望他们更改天气预报的指数,则服务还会提供4天的天气。 0 is today, 1 is tomorrow and so on. 0是今天,1是明天,依此类推。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM