访问特定的JSON数据

时间:2015-06-24 16:46:49

标签: jquery ajax json getjson

我有一个JSON数据Feed,我试图访问特定的值,似乎无法让它工作。

这是返回的JSON数据。

{"pollenForecast":{"zip":"37201","city":"NASHVILLE","state":"TN","forecast":[3.9,3.9,3.6,0.5],"pp":"Grass and Poplar/Cottonwood.","timestamp":"Jun 24, 2015 11:50:34 AM"},
"weatherForecast":{"date":"Jun 24, 2015 12:38:00 PM","city":"Nashville","state":"TN","zip":"37201","forecast":[
{"lowF":71,"highF":93,"iconDay":"3000","iconNight":"3100","skyDay":30,"skyNight":31,"phraseDay":"Partly Cloudy","phraseNight":"Clear","date":"Jun 24, 2015 12:00:00 AM"},    
{"lowF":74,"highF":97,"iconDay":"3200","iconNight":"7200","skyDay":32,"skyNight":4,"phraseDay":"Sunny","phraseNight":"Thunderstorms Late","date":"Jun 25, 2015 12:00:00 AM"},
{"lowF":71,"highF":87,"iconDay":"400","iconNight":"400","skyDay":4,"skyNight":4,"phraseDay":"Thunderstorms","phraseNight":"Thunderstorms","date":"Jun 26, 2015 12:00:00 AM"},
{"lowF":62,"highF":78,"iconDay":"6203","iconNight":"2900","skyDay":38,"skyNight":29,"phraseDay":"AM Thunderstorms","phraseNight":"Partly Cloudy","date":"Jun 27, 2015 12:00:00 AM"},
{"lowF":61,"highF":84,"iconDay":"3400","iconNight":"3100","skyDay":34,"skyNight":31,"phraseDay":"Mostly Sunny","phraseNight":"Clear","date":"Jun 28, 2015 12:00:00 AM"},
{"lowF":66,"highF":87,"iconDay":"3400","iconNight":"3300","skyDay":34,"skyNight":33,"phraseDay":"Mostly Sunny","phraseNight":"Mostly Clear","date":"Jun 29, 2015 12:00:00 AM"}]},"result":true}

我正试图访问括号内的'pollenForecast' - '预测'数字,似乎无法正常工作。

谢谢..

function pollen_quality() {
    $.ajax({ 
        type: 'GET', 
        url: '<?php echo $pollen_quality_url; ?>', 
        dataType: 'json',
        success: function(pollen) {
            pollen = pollen.replace(/[\[\]']+/g,'');
            var pollenForecast = pollen.split(':');;
            pollenForecast = pollenForecast[5].split(',');
            //build pollen quality from pollen forecast value
            if (pollenForecast[0] == null) {
                html = '<h1><?php echo $kiosk_pollen_quality_title ?></h1><p class="no_air_quality_data">No Data</p>';
            }
            else {
                if ((pollenForecast[0] >= 0) && (pollenForecast[0] <= 2.4)) {
                    html = '<h1><?php echo $kiosk_pollen_quality_title ?></h1><p style="color: #00FF00;">Low</p>';
                }
                else if ((pollenForecast[0] >= 2.5) && (pollenForecast[0] <= 4.8)) {
                    html = '<h1><?php echo $kiosk_pollen_quality_title ?></h1><p style="color: #99FF00;">Low-Medium</p>';
                }
                else if ((pollenForecast[0] >= 4.9) && (pollenForecast[0] <= 7.2)) {
                    html = '<h1><?php echo $kiosk_pollen_quality_title ?></h1><p style="color: #FFFF00;">Medium</p>';
                }
                else if ((pollenForecast[0] >= 7.3) && (pollenForecast[0] <= 9.6)) {
                    html = '<h1><?php echo $kiosk_pollen_quality_title ?></h1><p style="color: #FF9900;">Medium-High</p>';
                }
                else { //pollen level 9.7 - 12.0
                    html = '<h1><?php echo $kiosk_pollen_quality_title ?></h1><p style="color: #FFFFFF;">High</p>';
                }
            }
            $('#pollen_quality').html(html);
        }
    });
}

2 个答案:

答案 0 :(得分:1)

例如,在使用jQuery.post()时(并不重要,假设返回的JSON位于data中),您可以按如下方式获取pollenForeCast

$.post( "ajax/test.html", function( data ) {
  var pollenForeCasts = data.pollenForeCast.forecast;
  for (var i = 0; i < pollenForeCasts.length; i++) {
    alert(pollenForeCasts[i]);
});

如果我没弄错的话。

答案 1 :(得分:0)

您可以使用以下方式访问它:

pollenForecast.forecast

= [3.9, 3.9, 3.6, 0.5]

要访问此结果数组:

pollenForecast.forecast[0]

= 3.9

如果你想遍历它,那么只需使用:

var forecast = pollenForecast.forecast,
    forecastlen = forecast.length;

for(var i = 0; i <= forecastlen; i++){
  console.log(forecast[i]);
}