使用 json 保留格式

我已将 Mysql 数据库中的数据格式化如下:


[start]

do this

[working]

do that

and thad

not this

[end]

do this

and this

我使用这个 PHP 脚本获取数据:


$result = mysqli_query($conn, "SELECT * FROM ....");

$data = array();

while($row = mysqli_fetch_assoc($result))

{

$data[] = $row;

}


echo json_encode($data);

然后我在前端显示它:


json[0].columnName

我得到的是:


[start] do this [working] do that and thad not this [end] do this and this

我认为由于使用 json 格式丢失。是否可以将数据库中的格式保留到前端?谢谢


翻过高山走不出你
浏览 94回答 1
1回答

牛魔王的故事

我尝试在本地模拟您的问题<?php$mysqli = new mysqli("localhost","root","root","stackoverflow");$result = mysqli_query($mysqli, "SELECT * FROM data");$data = array();while($row = mysqli_fetch_assoc($result)){&nbsp; &nbsp; $data[] = $row;}?><div id="test"></div><script>var data = <?php echo json_encode($data); ?>;console.log(data&nbsp; &nbsp; );document.getElementById('test').innerHTML = data[0].Location.split('\n').join('<br>');</script>按预期输出:[start]do this[working]do thatand thadnot this[end]do thisand this所以你需要json[0].columnName.split('\n').join('<br>')在你的js代码中做:)
打开App,查看更多内容
随时随地看视频慕课网APP