[英]Read and parse JSON by variable
I have a value stored as thekeyvalue. 我有一个值存储为键值。 In this case "12F" for example. 在这种情况下,例如“ 12F”。 I want to see if it exists in my JSON. 我想看看它是否存在于我的JSON中。 If so, I'd like to grab the PNG value. 如果是这样,我想获取PNG值。 If not, just send a message back 如果没有,请发回一条消息
JSON JSON
{
"Property": "99",
"12F": {
"png": "12-74"
},
"13F": {
"png": "12-74"
}
}
JQUERY 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
}
});
This is how your success function should be. 这就是您的成功职能应该如何。 Please check. 请检查。
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);
if(json["12F"]])
return json["12F"].png;
else
return "";
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.