As per title, is it considered a good practice to put HTML in JSON? The reason I need to do this is because I would like to have a custom dropdown where the list is coming from the user input, and the json looks like so:
{ listTitle: 'Tasks', listHtml: '<ul><li></li>...</ul>' }
and I have the foreach as following (keep in mind this is a stripped down version of my code, validation is in place, but for the sake of this question I took them out)
$list = /** Code to grab 'Tasks' list and its title from mysql **/;
$title = 'Tasks';
$listHtml = '';
foreach($list as $content) { $listHtml .= '<li>' . htmlspecialchars($content, ENT_QUOTES, 'UTF-8') . '</li>'; }
exit(json_encode(array(
'title' => $title, 'listHtml' => '<ul>' . $listHtml . '</ul>'
)));
My worry is that there might be some special characters that might break the JSON String. Please help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…