我想找出一种方法来从 ajax 查询检索到的 JSON 字符串中检索 bool 值。我可以访问字符串和整数值,但任何 bool 值都会作为 undefined
返回到控制台。
这是我解析ajax返回数据的脚本。
success: function(data){
let results = jQuery.parseJSON(data);
var outside = results.outside;
console.log(outside);
if(outside){
console.log("true");
}
}
这是我的 JSON 字符串的示例:
{
"glossary": "alphabet",
"store": "Henry",
"outside": true
}
我可以返回字符串值,但不能返回 "outside"
的 bool 值。我知道它正在被 ajax 调用检索,因为在测试中我将 data
打印到控制台并且我可以看到它的值为 true
。
最佳答案
自 jQuery 3.0 起,$.parseJSON 已弃用。要解析 JSON 字符串,请改用 native JSON.parse 方法。
来自:http://api.jquery.com/jquery.parsejson/
所以,你需要使用:JSON.parse 方法 https://www.w3schools.com/js/js_json_parse.asp
关于javascript - 返回特定键的未定义 bool 值的 JSON 字符串的 JSON.parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55855794/