2013-10-03 120 views
0

我知道這個問題已被問及幾次,我已經看到了答案,但它已經幫了我很多,但我需要解決更多的問題。 所以問題是: 我需要建立與PHP的JSON文件。 這裏的樣子,我需要我的JSON文件:使用php創建json數據文件的圖片庫文件

{ 
"fashion":[ 
{ 
     "alt":"Alisa", 
     "src":"img/fashion/Alisa/kubik.jpg", 
     "class":"albumItem", 
     "id":"FashionAlbum001", 
     "itemNum":0, 
     "album":[ 
      { 
       "alt":"albumImg1", 
       "src":"img/fashion/Alisa/alisa1.jpg" 
      }, 
      { 
       "alt":"albumImg1", 
       "src":"img/fashion/Alisa/alisa5.jpg" 
      }, 
      { 
       "alt":"albumImg1", 
       "src":"img/fashion/Alisa/alisa7.jpg" 
      } 
     ] 
    }, 
    { 
     "alt":"2-Addis", 
     "src":"img/fashion/2-Addis/kubik.jpg", 
     "class":"albumItem", 
     "id":"FashionAlbum002", 
     "itemNum":1, 
     "album":[ 
      { 
       "alt":"albumImg1", 
       "src":"img/fashion/2-Addis/addis1.jpg" 
      }, 
      { 
       "alt":"albumImg4", 
       "src":"img/fashion/2-Addis/addis4.jpg" 
      }] } ] } 

等等... 我不能找出如何讓每一張專輯裏面 圖像列表這是一個PHP函數有

function buildJson(){ 
$json = json_encode(array(
    "Fashion" => array(
     "alt" => "Alisa", 
     "src" => "img/fashion/Alisa/kubik.jpg", 
     "id" => "FashionAlbum001", 
     "itemNum"=>"1", 
     "album"=>array(
      "src"=>"img/fashion/Alisa/alisa1.jpg",   
     ), 
     array(
      "src"=>"img/fashion/Alisa/alisa5.jpg",   
     ), 
     array(
      "src"=>"img/fashion/Alisa/alisa7.jpg",   
     ), 

    ) 
)); 

echo $json; 

} ,但我得到的JSON像這樣的:

{ 
"Fashion": { 
    "0": { 
     "src": "img/fashion/Alisa/alisa2.jpg" 
    }, 
    "1": { 
     "src": "img/fashion/Alisa/alisa3.jpg" 
    }, 
    "alt": "Alisa", 
    "src": "img/fashion/Alisa/kubik.jpg", 
    "id": "FashionAlbum001", 
    "itemNum": "0", 
    "album": { 
     "src": "img/fashion/Alisa/alisa1.jpg" 
    } 
} 

}

它怎麼可能修復它? 謝謝!

回答

0

您在嵌套專輯陣列

固定碼麻煩

function buildJson(){ 
     $json = json_encode(
      array(
       "Fashion" => array(
        "alt" => "Alisa", 
        "src" => "img/fashion/Alisa/kubik.jpg", 
        "id" => "FashionAlbum001", 
        "itemNum"=>"1", 

        // nesting error here 

        "album"=> array(
         array("src"=>"img/fashion/Alisa/alisa1.jpg"), 
         array("src"=>"img/fashion/Alisa/alisa5.jpg"), 
         array("src"=>"img/fashion/Alisa/alisa7.jpg") 

        ) 
       ) 
      ) 
     ); 

     echo $json; 
    } 
2

請更加關注你寫:)試試你的JSON文件的版本是否正確解碼,並將其與一個代碼你寫了。你應該看到一些差異。

您的問題ir後面的內容album鍵。您只將一個值分配給數組,而不是分配數組數組。

這是要走的路:

"album" => array(
    array("src" => "img/fashion/Alisa/alisa1.jpg"), 
    array("src" => "img/fashion/Alisa/alisa5.jpg"), 
    array("src" => "img/fashion/Alisa/alisa7.jpg"), 
), 
+0

偉大的,它的工作。謝謝。 – Karb