2011-08-18 192 views
5

如何擺脫下面的json處理的括號?刪除json中的括號?

[{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"}] 

結果以上由下面的類加工成一個陣列,

function handle_upload($upload_directory) 
    { 
     # Loop the code according to the number of files. 
     for($i = 1; $i <= $this->total; $i++) 
     { 
      ... 

      if ($this->file->save($upload_directory.$name_filtered.'.'.$file_extension , $i-1)) 
      { 
       $message[] = array('success'=>true,'filename'=>$name_filtered.'.'.$file_extension); 
      } 
      else 
      { 
       $message[] = array('error'=> 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered'); 
      } 
     } 

     return $message; 
    } 

然後我使用json_encode打開陣列分成JSON格式,

$uploader = new uploader(); 
$result = $uploader->handle_upload('uploads/'); 

echo htmlspecialchars(json_encode($result), ENT_NOQUOTES); 

但我只需要這在我的結果沒有括號,

{"success":true,"filename":"bialding_and_rebialding_plymouth02.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth03.jpg"},{"success":true,"filename":"bialding_and_rebialding_plymouth04.jpg"} 
+2

沒有括號,你的json字符串將是無效的。一個JSON字符串可以只包含一個值。使用括號,它是一個數組或對象。沒有括號,它是一系列單獨的逗號分隔對象 - 無效。 –

+1

不再有效的json。 – hop

+0

爲什麼你不想'']'?你打算怎麼處理結果字符串? –

回答

16

str_replace(array('[', ']'), '', htmlspecialchars(json_encode($result), ENT_NOQUOTES));

+0

謝謝。我也想到了! – laukok