2013-05-02 70 views
0

我使用下面的数据从MySQL转换成JSON:太多的JSON信息

$sql = "select img_name from user_gallery_images where user_id=$_SESSION[user_id]"; 

     $response = array(); 
     $posts = array(); 

     $result = $mysqli->query($sql); 

     while($row = $result->fetch_array()){ 

      $file=$row['img_name']; 
      $fileDir = "gallery/$file.jpg"; 
      $posts[] = array('thumb'=> $fileDir, 'image'=> $fileDir); 

     } 

     $response['posts'] = $posts; 

     $fp = fopen('/home/public_html/users/'.$settings[username].'/gallery/gallery.json', 'w'); 
     $jsonData = stripslashes(json_encode($response)); 
     fwrite($fp, $jsonData); 
     fclose($fp); 

效果很好,并如创建

{"posts": 
[ 
{"thumb":"gallery/tess1367386438.jpg","image":"gallery/tess1367386438.jpg"}, 
{"thumb":"gallery/tess1367386538.jpg","image":"gallery/tess1367386538.jpg"} 
] 
} 

但是,在jQuery插件在我与使用也不会与外“上岗”集装箱阅读

问题:

我怎样才能剥离外部“帖子”在JSON容器只能产生:

[ 
{"thumb":"gallery/tess1367386438.jpg","image":"gallery/tess1367386438.jpg"}, 
{"thumb":"gallery/tess1367386538.jpg","image":"gallery/tess1367386538.jpg"} 
] 

回答

3

尝试

$jsonData = json_encode($response['posts']); 
+0

为什么'stripslashes'? 'json_encode'提供斜线有一个原因 – zerkms 2013-05-02 05:28:01

+0

@zerlms OP在代码中,所以我假设他有它的原因,但是它并不是真的需要。 – 2013-05-02 05:28:59

+0

@SabeenMalik非常完美,非常感谢,我每天都在学习更多! – 2013-05-02 05:52:04