2014-01-16 99 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);