使用php为gallery文件创建json数据文件

时间:2013-10-03 11:17:16

标签: php json

我知道这个问题已经被问了几次,我已经看到了答案,但它已经帮助了我很多,但我需要解决另外一个问题。 所以问题是: 我需要用php构建json文件。 如何查看我需要的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"
    }
}

}

如何修复它? 谢谢!

2 个答案:

答案 0 :(得分:2)

请注意您正在编写的代码:)尝试解码json文件的正确版本并将其与您编写的文件进行比较。你应该看到一些差异。

您的问题是album键之后的问题。您只为数组分配一个值,而不是分配数组数组。

这是要走的路:

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

答案 1 :(得分: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;
    }