我使用以下内容将数据从mysql转换为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插件不会用外部“posts”容器读取它
问题:
如何剥离JSON中的外部“posts”容器以仅生成:
[
{"thumb":"gallery/tess1367386438.jpg","image":"gallery/tess1367386438.jpg"},
{"thumb":"gallery/tess1367386538.jpg","image":"gallery/tess1367386538.jpg"}
]
答案 0 :(得分:3)
尝试
$jsonData = json_encode($response['posts']);