工学1号馆

home

« | 返回首页 | »

前端显示后台传递的json字符串问题

By Wu Yudong on September 22, 2019

今天在项目中的一个需求就是:在前端显示后台传递的json字符串问题,看似简单的一个需求花了点时间,我的方案如下,不排除还有其他的方法,但是时间有限,以后再优化吧

后台代码:

$book_info = Db::name('project')->where('identify', $param['identify'])->find();
$array = [];
$array['book_id'] = $book_info['project_id'];
$array['book_name'] = $book_info['project_name'];
$array['identify'] = $book_info['identify'];
$array['description'] = $book_info['description'];
$array['doc_count'] = $book_info['doc_count'];
$array['comment_count'] = $book_info['comment_count'];
$array['create_time'] = $book_info['create_time'];
$json = json_encode($array, 320);
$this->assign('json', $json);

前端代码:

window.book = {$json|raw};

320确保中文正常显示,raw函数确保json中的双引号正常显示

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)