Accessing Specific JSON Data

85 views Asked by At

I have a JSON data feed that I am trying to access specific values, and can't seem to get it working.

Here is the JSON data returned.

{"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}

I am trying to access the 'pollenForecast' - 'forecast' numbers inside the brackets, and can't seem to get anything to work.

Thanks..

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

There are 2 answers

0
user2609980 On

For example while using jQuery.post() (does not really matter, assume returned JSON is in data) you can get pollenForeCast as follows:

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

If I am not mistaken.

0
Naim Rajiv On

You can access it by using:

pollenForecast.forecast

= [3.9, 3.9, 3.6, 0.5]

To get access this resulted array:

pollenForecast.forecast[0]

= 3.9

If you want traverse it then just use:

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

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