2014-01-16 135 views
0

我無法按照以下格式返回json,因爲它是超大圖片庫插件所需的格式。如何以下列格式返回json

   [ {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-1.jpg' title : 'Image Credit: Maria Kazvan', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-1.jpg', url : 'http://www.nonsensesociety.com/2011/04/maria-kazvan/'}, 
                {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-2.jpg', title : 'Image Credit: Maria Kazvan', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-2.jpg', url : 'http://www.nonsensesociety.com/2011/04/maria-kazvan/'}, 
                {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-3.jpg', title : 'Image Credit: Maria Kazvan', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-3.jpg', url : 'http://www.nonsensesociety.com/2011/04/maria-kazvan/'}, 
                {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/wojno-1.jpg', title : 'Image Credit: Colin Wojno', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/wojno-1.jpg', url : 'http://www.nonsensesociety.com/2011/03/colin/'}, 
                { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', title: 'Image Credit: Brooke Shaden', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-2.jpg', url: 'http://www.nonsensesociety.com/2011/06/brooke-shaden/' }, 
                { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-2.jpg', title: 'Image Credit: Maria Kazvan', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-2.jpg', url: 'http://www.nonsensesociety.com/2011/04/maria-kazvan/' }, 
                { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-3.jpg', title: 'Image Credit: Maria Kazvan', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-3.jpg', url: 'http://www.nonsensesociety.com/2011/04/maria-kazvan/' }, 
                { image: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/wojno-1.jpg', title: 'Image Credit: Colin Wojno', thumb: 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/wojno-1.jpg', url: 'http://www.nonsensesociety.com/2011/03/colin/' },     
                {image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-3.jpg', title : 'Image Credit: Brooke Shaden', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-3.jpg', url : 'http://www.nonsensesociety.com/2011/06/brooke-shaden/'} 

我用下面的代碼嘗試過,但沒有成功。

$sql1 = "select file,title from multi_uploads where object_id='$postid'"; 

        $result=mysql_query($sql1 ,$conn); 

          if (!$result) 
          { 
           die('Invalid query: ' . mysql_error()); 
          } 



                 $finaloutput=array(); 

          while($row=mysql_fetch_row($result)) 
          { 
                    $output=array(); 

        $image='image : \'http://xxxxxxxxx.in/sample/newedp/admin/files/'.$row[0].'\''; 
        $title='title : \''.$row[1].'\''; 
         $thumb='thumb : \'http://xxxxxxxxxxxx.in/sample/newedp/admin/files/thumbnail/'.$row[0].'\''; 
         $url='url : \'http://xxxxxxxxxxxxx.in/sample/newedp/admin/files/'.$row[0].'\''; 
         $onerow='{ '.$image.','.$title.','.$thumb.','.$url.'}'; 
        array_push($finaloutput,json_encode($onerow)); 

          echo json_encode($finaloutput); 

給我打印json格式的示例代碼。

+1

嘗試好好準備陣列。然後使用json_encode。 –

+0

看看一些例子[這裏](http://in3.php.net/json_encode)&再試一次。 – Rikesh

+0

採取回聲json_encode($ finaloutput); while循環之外 –

回答

0

嘗試

json_encode($finaloutput,JSON_UNESCAPED_SLASHES) 

代替

json_encode($finaloutput); 
+0

不幸的是,這並沒有解決嘗試從手動構建JSON的JSON核心問題,然後將字符串(甚至不是有效的JSON)推送到稍後json_encoded的數組中。 – user2864740

0

的核心問題是該代碼是使用json_encode正確 - json_encode需要PHP 對象圖(陣列和值,如由print_r顯示)並將其編碼爲JSON。

相反,發佈的代碼嘗試手動構建JSON(oops!),然後嘗試對手動構建的JSON(oops!)進行json_encode,然後嘗試對所有垃圾(oops!)的數組json_encode。最終的結果是,儘管最終的json_encode將會是有效的JSON,但它看起來像預期的格式沒有任何

取而代之的是,代碼準備對象圖第一個並且只在其上運行json_encode一次。

$items = array(); 

for (each row) { 
    // DO NOT build the "JSON" for the item manually; it will be 
    // encoded later when the object graph is traversed by json_encode. 
    // (Look how much cleaner the code already looks!) 
    $item = array(
     "image" => "http://panacya.in/sample/newedp/admin/files/".$row[0], 
     "title" => "".$row[1] 
); 

    // Add the item to the object graph, but DO NOT encode it; it will be 
    // encoded later when the object graph is traversed by json_encode. 
    // (If the item is encoded here then the later call to json_encode 
    // will be encoding an array of strings and not an array objects.) 
    push_array($items, $item); 
} 

// At the end, use json_encode ONCE on the entire object graph root. 
// All the reachable values (as seen with print_r) will be encoded as JSON. 
$result = json_encode($items); 
1
$output = []; 
while($row = mysql_fetch_row($result)) { 
    $array = []; 
    $array["image"] = "http://panacya.in/sample/newedp/admin/files/{$row[0]}"; 
    $array["title"] = $row[1]; 
    ... 
    $output[] = $array; 
} 
echo json_encode($output);