列出JSON中的所有文件和目录

时间:2012-02-07 01:54:24

标签: php jquery json directory

我想要在JSON中列出我的所有文件和文件夹,但我需要能够使用jQuery调用文件

基本上我所拥有的是以下

public function listproductimagedirectory($rootPath) {
    $pathStack = array($rootPath);
    $contentsRoot = array();
    $contents = &$contentsRoot;
    while ($path = array_pop($pathStack)) {
       $contents[basename($path)] = array();
       $contents = &$contents[basename($path)];
       foreach (scandir($path) as $filename) {
           if ('.' != substr($filename, 0, 1)) {
               $newPath = $path.'/'.$filename;
               if (is_dir($newPath)) {
                   array_push($pathStack, $newPath);
                   $contents[basename($newPath)] = array();
               } else {
                   $contents[basename($filename)] = $newPath;
               }
           }
       }
    }
    return $contentsRoot[basename($rootPath)];
    }

输出

{"giftcard":{"31791_319x242_0.jpg":"..\/products\/giftcard\/31791_319x242_0.jpg","7520921_0_9999_lge_v1_m56577569834701600.jpg":"..\/products\/giftcard\/7520921_0_9999_lge_v1_m56577569834701600.jpg","AMF.jpg":"..\/products\/giftcard\/AMF.jpg","accor.jpg":"..\/products\/giftcard\/accor.jpg","accorhotel.png":"..\/products\/giftcard\/accorhotel.png","cellarmasters.png":"..\/products\/giftcard\/cellarmasters.png","dusk.png":"..\/products\/giftcard\/dusk.png","giftca_100ath_lrg.png":"..\/products\/giftcard\/giftca_100ath_lrg.png","giftca_100goo_lrg.png":"..\/products\/giftcard\/giftca_100goo_lrg.png","giftca_100sun_lrg.png":"..\/products\/giftcard\/giftca_100sun_lrg.png","giftca_20itu_lrg.png":"..\/products\/giftcard\/giftca_20itu_lrg.png","giftca_25abc_lrg.png":"..\/products\/giftcard\/giftca_25abc_lrg.png","giftca_25jay_lrg.png":"..\/products\/giftcard\/giftca_25jay_lrg.png","giftca_30bra_lrg.png":"..\/products\/giftcard\/giftca_30bra_lrg.png","giftca_30div_lrg.png":"..\/products\/giftcard\/giftca_30div_lrg.png","giftca_40ebg_lrg.png":"..\/products\/giftcard\/giftca_40ebg_lrg.png","giftca_50col_lrg.png":"..\/products\/giftcard\/giftca_50col_lrg.png","giftca_50cou_lrg.png":"..\/products\/giftcard\/giftca_50cou_lrg.png","giftca_50dot_lrg.png":"..\/products\/giftcard\/giftca_50dot_lrg.png","giftca_50dym_lrg.png":"..\/products\/giftcard\/giftca_50dym_lrg.png","giftca_50hoy_lrg.png":"..\/products\/giftcard\/giftca_50hoy_lrg.png","giftca_50itu_lrg.png":"..\/products\/giftcard\/giftca_50itu_lrg.png","giftca_50jac_lrg.png":"..\/products\/giftcard\/giftca_50jac_lrg.png","giftca_50jea_lrg.png":"..\/products\/giftcard\/giftca_50jea_lrg.png","giftca_50pet_lrg.png":"..\/products\/giftcard\/giftca_50pet_lrg.png","giftca_50por_lrg.png":"..\/products\/giftcard\/giftca_50por_lrg.png","giftca_50reb_lrg.png":"..\/products\/giftcard\/giftca_50reb_lrg.png","giftca_50san_lrg.png":"..\/products\/giftcard\/giftca_50san_lrg.png","isubscribe.jpg":"..\/products\/giftcard\/isubscribe.jpg","isubscribe.png":"..\/products\/giftcard\/isubscribe.png","peteralexander.png":"..\/products\/giftcard\/peteralexander.png","rydges hotel.png":"..\/products\/giftcard\/rydges hotel.png","smiggle.png":"..\/products\/giftcard\/smiggle.png","sportsgirl.png":"..\/products\/giftcard\/sportsgirl.png","sussan.png":"..\/products\/giftcard\/sussan.png","wiggles.png":"..\/products\/giftcard\/wiggles.png"}}

我看到了什么,我可能错了,但没有任何东西允许我告诉数组的ID,例如我用来通过使用每个函数调用JSON并使用类似的东西

obj.title会告诉我它是json行的标题。

1 个答案:

答案 0 :(得分:1)

将你的对象放在一个数组中...所以你会有..

var myobject = { giftcard: [ {filename: "image1.png", path: "path"}, {filename: "image2.png", path: "path""}, {filename: "image3.png", path: "path"} etc...]}

然后你可以:

$.each(myobject.giftcard, function(index, image) { 
   image.filename
   image.path
});