這是我原來的JSON文件「幽靈」斜線,將項目添加到JSON文件?我該怎麼辦?
data.json(前)
[
{ "thumb": "../userfiles/img/min/m_1.jpg", "image": "../userfiles/img/1.jpg", "title": "Image 1", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_2.jpg", "image": "../userfiles/img/2.jpg", "title": "Image 2", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_3.jpg", "image": "../userfiles/img/3.jpg", "title": "Image 3", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_4.jpg", "image": "../userfiles/img/4.jpg", "title": "Image 4", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_5.jpg", "image": "../userfiles/img/5.jpg", "title": "Image 5", "folder": "Folder 1" }
]
這是我的PHP文件,將一個元素添加到文件data.jon
add_json.php
<?
$dir_thumb = '../userfiles/img/thumb/6_m.jpg';
$dir_img = '../userfiles/img/6.jpg';
$title = 'img_6';
$tipo_image = 'news';
$json = file_get_contents('data.json');
$data = json_decode($json);
$data[] = array(
'thumb'=> $dir_thumb,
'image'=> $dir_img,
'title'=> $title,
'folder'=> $tipo_image
);
file_put_contents('data.json', json_encode($data));
?>
這是生成的文件d ata.json
data.json(後執行文件add_json.php)
[
{"thumb":"..\/userfiles\/img\/min\/m_1.jpg","image":"..\/userfiles\/img\/1.jpg","title":"Image 1","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_2.jpg","image":"..\/userfiles\/img\/2.jpg","title":"Image 2","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_3.jpg","image":"..\/userfiles\/img\/3.jpg","title":"Image 3","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_4.jpg","image":"..\/userfiles\/img\/4.jpg","title":"Image 4","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_5.jpg","image":"..\/userfiles\/img\/5.jpg","title":"Image 5","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/thumb\/6_m.jpg","image":"..\/userfiles\/img\/6.jpg","title":"img_6","folder":"news"}
]
我做什麼,除去反斜槓?感謝您的幫助
這只是PHP矯枉過正。不用擔心,JSON仍然有效,並且字符串具有**完全**的值,而且沒有不必要的反斜槓。 –
或'JSON_UNESCAPED_SLASHES'如其他幾十個副本中的一個所述。 – mario
@ Francisco:如果下面的答案之一解決了您的問題,Stack Overflow的工作方式,您可以通過單擊左側的複選標記來「接受」答案。更多:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –