JSON到JavaScript字符串

use*_*596 -1 javascript string json

我已经看过很多关于如何将Javascript字符串转换为JSON的帖子,但没有任何关于如何将JSON对象转换为javascript字符串的帖子.

下面的代码片段在javascript控制台上显示时会返回正确的值,但是,当我尝试对JSON.element进行字符串比较时,它会失败.

这是URL请求返回的JSON.

{
  "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": []
}
Run Code Online (Sandbox Code Playgroud)

这是代码片段:

       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");
                  }
       });
Run Code Online (Sandbox Code Playgroud)

console.log(jstr)返回true.console.log(jonline.connected)返回true.

下面的比较总是落到其他地方.

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;
}
Run Code Online (Sandbox Code Playgroud)

我很确定JSON对象需要转换为javascript字符串,但我无法找到能够做到这一点的方法.我发现的所有搜索都是关于从javascript到JSON的讨论.

在此先感谢您的帮助!

Ste*_*wer 5

jonline.connect!= jonline.connected

更改jstr = jonline.connect;jstr = jonline.connected;

您不需要将JSON String转换为JS对象,因为jQuery的$ .getJSON已经自动转换它.

此外,斜视在下面的注释中指出,您还将对象的布尔值true与字符串"true"进行比较.那些不是一回事.

更改if (jstr == "true") {if (jstr == true) {

这是因为你"connected": true的json中有一行,而不是"connected": "true".

根据您的评论,似乎另一个问题是,您用于根据结果设置html内容的代码是在执行JSON回调之前进行比较.你在$ .getJSON()之后追加的所有东西都会即时发生,即使json还没有加载,beucase它是一个异步函数.

您可以在回调中添加代码:

if (jstr == "true") {
   online_status = true;
   document.getElementById("temp").innerHTML = "Online";
   document.getElementById("tstamp").innerHTML = "Last heard from (GMT) Date/Time          -> " + jstr;
} else { 
   online_status = false;
   jstr = jonline.last_heard;

   document.getElementById("temp").innerHTML = "NOT Online";
   document.getElementById("tstamp").innerHTML = "Last heard from (GMT) Date/Time          -> " + dt_str;
}
Run Code Online (Sandbox Code Playgroud)

或者,如果online_status是一个变量,你可以在没有你的功能的情况下引用它,如果我正确理解你的注释,你可以用你的online_status比较创建一个函数,并在你的$ .getJSON回调中调用它,在设置之后价值.

  • @ user3254596:[阅读此问答](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron).这会被问到一天多次. (2认同)