如何提取PHP JSON数组名称
我有一个返回JSON数组的PHP文件。我需要提取数组名,在本例中为*Regulatory_Guidance_Library*,jQuery作为如何提取PHP JSON数组名称,php,json,variables,jquery,Php,Json,Variables,Jquery,我有一个返回JSON数组的PHP文件。我需要提取数组名,在本例中为*Regulatory_Guidance_Library*,jQuery作为$('#navHeaderTitle').text(arrayName)的变量。我不知道这是怎么做到的 谢谢你的帮助 HTML文档中的我的函数: function loadNav(url, container, appendE) { $.getJSON(url, function(data){ var
$('#navHeaderTitle').text(arrayName)的变量我的函数的代码>。我不知道这是怎么做到的
谢谢你的帮助
HTML文档中的我的函数:
function loadNav(url, container, appendE) {
$.getJSON(url, function(data){
var arrayName = "";
$.each(data.Regulatory_Guidance_Library, function(){
var newItem = $('#' + container).clone();
// Now fill in the fields with the data
newItem.find("h1").text(this.label);
newItem.find("h2").text(this.title);
newItem.find("p").text(this.description);
// And add the new list item to the page
newItem.children().appendTo('#' + appendE)
});
$('#navHeaderTitle').text(arrayName);
//transition(pageName, "show");
});
};
服务对象:
<?php
//MySQL Database Connect
include 'andaerologin.php';
mysql_select_db("andaero");
$sql=mysql_query("select * from regulatory_list");
$output = new stdClass();
while($row = mysql_fetch_assoc($sql)) {
$output->Regulatory_Guidance_Library[] = $row;
}
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
echo (json_encode($output));
mysql_close();
?>
修改php:
$output = array();
/* used "key" as name...could change to suit your app*/
$output['key']='Regulatory_Guidance_Library';
while($row = mysql_fetch_assoc($sql)) {
$output['items'][]= $row;
}
阿贾克斯:
另一个答案可能更有用,但如果您想使用javascript获取对象属性的名称,可以执行以下操作:
for (var name in data) {
alert(name);
}
在您的情况下,由于您只有一个属性,您可以执行以下操作:
for (var name in data) {
$('#navHeaderTitle').text(name);
}
美丽的!你帮了大忙。我把这东西拿下来。再来一次
for (var name in data) {
$('#navHeaderTitle').text(name);
}