我有一个看起来像这样的 json 对象:
{
"id": "id_1636844335636",
"title": "Emmy",
"start": "2021-11-09",
"end": "2021-11-10",
"description": "<img class=\"img-fluid\" src=\"data\/uploads\/content_media_6190432d1512a.jpg\" alt=\"\" width=\"196\" height=\"130\" \/>",
"allDay": true,
"order": 1
}
读取 json 数据时,出现 js 错误“Uncaught SyntaxError: Unexpected identifier”
当我在描述中替换\"为时',一切都很好。
// no error when changing to this:
"description": "<img class='img-fluid' src='data\/uploads\/content_media_6190432d1512a.jpg' alt='' width=\"196\" height='130' \/>",
我在 fullcalendar 脚本中使用这些 json。我真的不知道为什么转义字符\"会导致问题。json 中的语法是否以某种方式错误?
顺便说一句:这就是 json 是如何由 php 创建的:
$file = 'data/'.$id.'.json';
$jsonData = json_encode($array, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
file_put_contents($file, $jsonData);
更新:这是读取 json 数据的 Fullcalendar 脚本的一部分:
events: [
<?php
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('data/rooster/')); // read all files from all the years dirs
$roosterfiles = [];
foreach ($rii as $roosterfile) {
if ($roosterfile->isDir()){
continue;
}
$roosterfiles[] = $roosterfile->getPathname();
}
//$roosterfiles = glob('data/rooster/2021/*.json'); // all roosterfiles in events folder
foreach($roosterfiles as $roosterfile) {
$objs[] = json_decode(file_get_contents($roosterfile), true); // decode to php assoc array
}
foreach($objs as $key => $val) {
?>
{
// Eventdata
id: "<?php echo $val['id']; ?>", // id
groupId: "<?php echo $val['groupId']; ?>", // groupId integer only!
category: "<?php echo $val['category']; ?>", // category
title: "<?php echo $val['title']; ?>", // title
backgroundColor: "<?php echo $val['backgroundColor']; ?>", // bg color
textColor: "<?php echo $val['textColor']; ?>", // textcolor
start: "<?php echo $val['start']; ?>", // start
end: "<?php echo $val['end']; ?>", // end
description: "<?php echo $val['description']; ?>", // description
url: "<?php echo $val['url']; ?>", // url
borderColor: "<?php echo $val['backgroundColor']; ?>", // optional; same color as background
allDay: "<?php echo $val['allDay']; ?>", // default all day event
order: "<?php echo $val['order']; ?>", // order by category
},
<?php
} // end foreach
?>
],