我有一个存储为keyvalue的值。例如,在这种情况下,“12F”。我想看看它是否存在于我的JSON中。如果是这样,我想获取PNG值。如果没有,只是发送一条消息回通过变量读取和解析JSON
JSON
{
"Property": "99",
"12F": {
"png": "12-74"
},
"13F": {
"png": "12-74"
}
}
JQUERY
var sourceurl = '../floorplan-data.json';
var thekeyvalue = $("#finalkey").text();
//start ajax request
$.ajax({
url: sourceurl,
//force to handle it as text
dataType: "text",
error:
function(){
//error
},
success: function(data) {
var json = $.parseJSON(data);
console.log(json.thekeyvalue); //having trouble here
}
});
你为什么使用'dataType:“text”'? – SLaks
试试'json [thekeyvalue]'。说明:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_Accessors – Will
omg严重这就是它,但是当它无法找到它时,它将“undefined”发送给成功功能而不是错误。如果不存在,我如何触发jquery。 – Omar