I there a way to read or get the key name(if my term is correct) in this case I’m referring to “homedata” or “faq”. I want to know if is “homedata or “faq” so that I can store it in a variable and use to compare.
for my jquery this is my script:
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");
}
});
Here is my json result:
[
{
"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"
}
}
]
You can use the dot notation
If your json response looks like
You can get the value under key as
EDIT
Ok I have made a crude fiddle here It will loop over your json response and write the name of “homedata” or “faq” keys to the div. Note that it will override each time, so the last key in the json array will show in the end. This is just to illustrate for you how to get at the key names. Hopefully this helps