我试图从我认为应该是JSON对象的值中获取值,但我开始认为它不是。
首先是我的代码:
这部分实际上调用了IBM Bluemix的气象服务,并返回应该是json对象的内容。
var weatherData;
function getWeather(latitude, longitude, startDate, endDate) {
// Prepare output in JSON format
var callURL = "https://REMOVEDFORSECURITYREASONS@twcservice.mybluemix.net/api/weather/v1/geocode/"+latitude+"/"+longitude+"/almanac/daily.json?units=e&start="+startDate+"&end="+endDate
request.get(callURL, {
json: true
},
function (error, response, body) {
if(error){
console.log(error);
} else {
//var meta = body["metadata"];
//var id = meta["transaction_id"];
weatherData = body;
}
});
}
这部分应该解析来自sql数据库的一些数据,然后解析天气数据合并它并输出它,如下面的代码所示,但如果我能得到我需要的天气数据,我可以做那部分
function publish(rows)
{
var timestamp = rows[i].timestamp;
var motestack = rows[i].motestack;
var temperature = rows[i].temperature;
var spcond = rows[i].spcond;
var ph = rows[i].ph;
var depth = rows[i].depth;
var power = rows[i].power;
var turbidity = rows[i].turbidity;
var piezoresistance = rows[i].piezoresistance
var mgl_odo_sat = rows[i].mgl_odo_sat
var longitude = "-82.49357"
var latitude = "34.5244887"
var startDate = "0312"
var endDate = "0312"
getWeather(latitude,longitude,startDate,endDate);
var almanac = weatherData;
console.log(almanac);
//deviceClient.publish("status","json",'{"d" : { "time" : \"'+timestamp+'\" , "mote" : \"'+motestack+'\" , "temp" : \"'+temperature+'\" , "spcond" : \"'+spcond+'\" , "ph" : \"'+ph+'\" , "depth" : \"'+depth+'\" , "turbidity" : \"'+turbidity+'\", "power" : \"'+power+'\" , "piezoresistance" : \"'+piezoresistance+'\" , "mgl_odo_sat" : \"'+mgl_odo_sat+'\", "latitude" : \"'+latitude+'\", "longitude" : \"'+longitude+'\" }}', 1);
//console.log("status","json",'{"d" : { "time" : \"'+timestamp+'\" , "mote" : \"'+motestack+'\" , "temp" : \"'+temperature+'\" , "spcond" : \"'+spcond+'\" , "ph" : \"'+ph+'\" , "depth" : \"'+depth+'\" , "turbidity" : \"'+turbidity+'\", "power" : \"'+power+'\" , "piezoresistance" : \"'+piezoresistance+'\" , "mgl_odo_sat" : \"'+mgl_odo_sat+'\", "latitude" : \"'+latitude+'\", "longitude" : \"'+longitude+'\" }}', 1);
i=i+1;
}
这是我从上面的代码得到的输出。我已经尝试了各种方法来获取密钥:值对但是我得到的错误就像"无法读取属性'某些关键'未定义"。如果我执行body.almanac_summaries之类的操作,我会获得almanac_summaries数据,但它包含在未定义的{}中,如下面的数据所示。
Connection established
Device Connected
Data received from Db:
undefined
{ metadata:
{ language: 'en-US',
transaction_id: '1491701143744:483526643',
version: '1',
latitude: 34.52,
longitude: -82.49,
units: 'e',
expire_time_gmt: 1491426760,
status_code: 200 },
almanac_summaries:
[ { class: 'almanac',
station_id: '380165',
station_name: 'ANDERSON',
almanac_dt: '0312',
interval: 'D',
avg_hi: 64,
avg_lo: 39,
record_hi: 86,
record_hi_yr: 1967,
record_lo: 20,
record_lo_yr: 1998,
mean_temp: 52,
avg_precip: 0.14,
avg_snow: 0,
record_period: 30 } ] }