I've seen many post on how to turn a Javascript string into a JSON, but not any that suggest how to turn a JSON object into a javascript string.
The code snippet below returns the correct values when they are displayed in the on the javascript console, however, when I attempt to do an string to JSON.element comparison, it fails.
Here's the JSON that's being returned by the URL request.
{
"id": "1e003033",
"name": "camera",
"last_app": null,
"last_ip_address": "192.168.0.27",
"last_heard": "2016-08-27T14:22:49.762Z",
"product_id": 6,
"connected": true,
"platform_id": 6,
"cellular": false,
"status": "normal",
"pinned_build_target": "0.5.2",
"variables": {
"lonlat": "string",
"speedmph": "string",
"sats": "string"
},
"functions": []
}
Here's the code snippet:
requestURL_O = "https://api.spark.io/v1/devices/" + deviceID + "/?access_token=" + accessToken;
$.getJSON(requestURL_O, function(jonline){
console.log(jonline.connected);
jstr = jonline.connected;
dt_str = jonline.last_heard;
console.log(jstr);
if (jstr == "true"){
online_status = true;
console.log("equal = TRUE");
}
else {
online_status = false;
jstr = jonline.last_heard;
console.log(jonline.last_heard);
console.log("equal = FALSE");
}
});
console.log(jstr) is returned at true. console.log(jonline.connected) is returned as true.
The comparison below always falls through to the else.
if (online_status) {
document.getElementById("temp").innerHTML = "Online";
document.getElementById("tstamp").innerHTML = "Last heard from (GMT) Date/Time -> " + jstr;
}
else {
document.getElementById("temp").innerHTML = "NOT Online";
document.getElementById("tstamp").innerHTML = "Last heard from (GMT) Date/Time -> " + dt_str;
}
I'm fairly certain the JSON object needs to be converted to a javascript string, but I've not been able to locate the method that will do that. All the searching I've found talks about going from javascript to JSON.
Thanks in advance for any assistance!