2012-03-29 4 views
0

JSON 배열을 반환하는 PHP 파일이 있습니다. 배열 이름을 추출해야합니다.이 예제에서는 * Regulatory_Guidance_Library *를 jQuery와 함께 사용하여 $('#navHeaderTitle').text(arrayName);의 var 함수로 추출해야합니다. 나는 이것이 어떻게 행해졌는지 모른다. 당신의 도움에 대한PHP JSON 배열 이름을 추출하는 방법

Thnx

는 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 :

<?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(); 
?> 

답변

1

수정 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; 
} 

AJAX :

$.getJSON(url, function(data){ 
     $.each(data.items, 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(data.key); 
     //transition(pageName, "show"); 
    }); 
+0

아름다운! Youve 큰 도움이되었습니다. 나는이 물건을 가져올거야. 다시 Thnx – CelticParser

1

다른 대답은 아마 더 도움이 될 것입니다,하지만 당신은 자바 스크립트와 객체의 속성의 이름을 얻으려면 당신이 할 수 있습니다

for (var name in data) { 
    alert(name); 
} 

를 귀하의 경우에는, 당신은 단지 하나 개의 속성을 가지고 있기 때문에 할 수있다 :

for (var name in data) { 
    $('#navHeaderTitle').text(name); 
}