2014-01-16 5 views
0

나는 supersized 이미지 갤러리 pluggin에 필요한대로 다음 형식의 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을 인쇄하는 샘플 코드를 제공합니다. json_encode는 PHP 객체 그래프 (배열과 값을 얻어,로 -

+1

시도가 제대로 배열을 제조한다. 그런 다음 json_encode를 사용하십시오. –

+0

[here] (http://in3.php.net/json_encode)의 예제를 살펴보고 다시 시도하십시오. – Rikesh

+0

걸릴 echo json_encode ($ finaloutput); while 루프의 외부 –

답변

0

json_encode($finaloutput,JSON_UNESCAPED_SLASHES) 

대신

json_encode($finaloutput); 
+0

불행히도 이것은 수동으로 문자열에서 JSON을 빌드하고 문자열 (유효한 JSON이 아닌)을 나중에 json_encoded 인 배열로 밀어 넣으려는 핵심적인 문제를 해결하지 못합니다. – user2864740

0

핵심 문제는 코드가 정확하게 json_encode를 사용 하지 있다는 시도 print_r으로 표시) JSON으로 인코딩합니다.

게시 된 코드는 수동으로 JSON을 빌드하려고 시도하고 (oops!) 수동으로 작성한 JSON (oops!)을 json_encode로 시도한 다음 json_encode에 모든 정크 배열 (oops!)을 시도합니다. 최종 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); 
관련 문제