如何以下列格式返回json

时间:2014-01-16 07:40:07

标签: php 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的示例代码。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

尝试

json_encode($finaloutput,JSON_UNESCAPED_SLASHES)

而不是

json_encode($finaloutput);

答案 2 :(得分:0)

核心问题是代码正确使用json_encode - json_encode采用PHP 对象图(数组和值,如由print_r显示并将其编码为JSON。

相反,发布的代码尝试手动构建JSON(oops!)然后尝试json_encode手动构建的JSON(oops!),然后尝试json_encode所有垃圾的数组(oops!)。最终结果是,虽然最终的json_encode它将是有效的JSON,但它看起来像 nothing 就像预期的格式一样。

相反,代码首先准备对象图 ,并且最后只在其上运行一次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);