2011-08-21 23 views

回答

3

$文件需要一個數組一樣的attr鍵:

$file = array("data" => "w-file1","attr" => array("rel" => "file")); 
echo json_encode($file); 

當然,你可以內嵌它:

echo json_encode(array("data" => "w-file1","attr" => array("rel" => "file"))); 

或者,有OOP方法:

$file = new stdClass(); 
$file->data = 'w-file'; 
$file->attr = new stdClass(); 
$file->attr->rel = "file"; 
echo json_encode($file); 
+1

+1 OOP方法 – johnluetke

+0

OOP方法是最簡單的我讀。謝謝。 – Radek

+0

@cwallenpoole:我在jsno_encode之前如何調試'$ file'裏面的內容? – Radek

1
$file = array(
    "data" => "w-file1", 
    "attr" => array("rel" => "file") 
); 
echo json_encode($file); 
2

你需要爲$文件和attR指定array

$file = array("data" => "w-file1","attr" => array("rel" => "file")); 
echo json_encode($file); 
相關問題