2012-03-29 11 views
0

JSON配列を返すPHPファイルがあります。この例では配列名を抽出する必要があります。* Regulatory_Guidance_Library * jQueryをvarとして使用し、$('#navHeaderTitle').text(arrayName);の関数です。私はこれがどのように行われたのか分かりません。PHP JSON配列の名前を抽出する方法

あなたの助けのためのthnxのHTMLドキュメント内

My機能:

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

美しい!あなたは大きな助けになりました。私はこのものを手に入れます。再びThnx – CelticParser

1

他の答えは、おそらくより便利ですが、あなたはJavaScriptを使用して、オブジェクトのプロパティの名前を取得したい場合は、あなたが行うことができます:

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

をお使いの場合は、1つのプロパティのみあなたを持っているので、することができます:

for (var name in data) { 
    $('#navHeaderTitle').text(name); 
} 
関連する問題