在这种情况下,我有一种读取或获取密钥名称的方法(如果我的术语是正确的),我指的是“homedata”或“faq”。我想知道是“homedata”还是“faq”,以便我可以将它存储在一个变量中并用于比较。对于我的 jquery,这是我的脚本:
var varHolder ="";
$.ajax({
type: "POST",
//data: data_json,
dataType: "json",
url: "sql_queries.php",
success: function(data) {
$.each(data, function(key, val) {
//read the index name here and store it to varholder
varHolder = data[key];
//then compare it like this
if(varHolder == "homedata"){
//write something to the dome
}else if(varHolder == "faq"){
//write something to the dome
}
});
},
error:function(){
console.log("error fetching data. please refresh the page");//alert("Send Email Failure,Please try again");
},
complete:function(){
console.log("complete");//alert("Send Email Failure,Please try again");
}
});
这是我的 json 结果:
[
{
"homedata":
{
"approve_title":"home bla blah",
"approve_desc":"this is the approve JSON"
}
},
{
"homedata":
{
"approve_title":"another home bla blah",
"approve_desc":"tthis is the approve JSON",
"approve_path":"backend/pdf/this is the approve JSON.pdf"
}
},
{
"faq":
{
"progproj_title":"whatever JSON",
"progproj_details":"sos sos sos"
}
}
]