【发布时间】:2015-06-24 16:46:48
【问题描述】:
我有一个 JSON 数据馈送,我正在尝试访问特定值,但似乎无法使其正常工作。
这是返回的 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);
}
});
}
【问题讨论】:
-
给我看一些代码。你试过什么?哪里失败了?
-
pollenForecast.forecast[0] 会给出 3.9,我想,我可以知道你的代码吗,参考这个jsfiddle.net/ZigmaEmpire/uyjLwh2b