作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我怎样才能从 MySQL 做 JSON pretty-print ?我用过 JSON_PRETTY_PRINT
在我的代码中,但它没有打印我所期望的。我目前的脚本是:
<?php
//open connection to mysql db
$connection = mysqli_connect("127.0.0.1","root","Kunal@7890","testdb") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select id,title,profilepic,created_at,url from news";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
print json_encode($rows, JSON_PRETTY_PRINT);
}
?>
[ { "id": "1", "title": "test", "profilepic": "0", "created_at": "2016-09-05 12:11:17", "url": "0" } ][ { "id": "1", "title": "test", "profilepic": "0", "created_at": "2016-09-05 12:11:17", "url": "0" }, { "id": "2", "title": "JCECE", "profilepic": "http:\/\/results.jharkhandeducation.net\/JCECEB\/JCECEB-Logo.jpg", "created_at": "2016-09-16 10:14:55", "url": "https:\/\/jcece.co.in\/" } ]
{
"news": [
{
"id": 36,
"name": "JCECE",
"image": null,
"status": " JCECE 2016 will be conducted on June 5, 2016 by JCECE Board, which is the exam conducting authority for the engineering entrance examination. JCECE 2016 will be conducted to offer admissions to undergraduate engineering courses at the participating institutes of JCECE 2016 in the state of Jharkhand. As of now, there are a total of 19 colleges (government+private) that will offer over 6000 B.E/B.Tech seats to aspiring candidates in Jharkhand.
Application Dates:16 Apr 2016 to 16 May 2016
Admit Card Date:11 May 2015
Exam Dates:05 Jun 2016
Result Date:01 Jul 2015 to 10 Jul 2015 ",
"profilePic": "http://results.jharkhandeducation.net/JCECEB/JCECEB-Logo.jpg",
"timeStamp": "1461323436930",
"url": "https://jcece.co.in/"
},
{
"id": 39,
"name": "THAPAR UNIVERSITY",
"image": null,
"status": "The details about the Thapar University B.Tech admission 2016 have been released. The admission will be held as per the JEE Main 2016 score but candidates will have to fill a separate application form for it. Interested candidates, who are also eligible, may access the link below to apply. The last date to submit the application form is 26 April 2016.
Last Date:26 Apr 2016",
"profilePic": "https://upload.wikimedia.org/wikipedia/commons/d/da/ThaparUniversityLogo.jpg",
"timeStamp": "1459595788930",
"url": "http://www.thapar.edu/"
},
]
最佳答案
json_encode($rows,JSON_PRETTY_PRINT);
返回带有换行符的美化数据。这对命令行输入很有帮助,但正如您发现的那样,在浏览器中看起来并不漂亮。浏览器将接受换行符作为源(因此,查看页面源确实会显示漂亮的 JSON),但它们不用于在浏览器中格式化输出。浏览器需要 HTML。
理想情况下,您应该使用 <pre>
标签来包装你的输出。这将在浏览器上以与在源中表示的方式相同的方式表示。
如果由于某种原因这还不够,您还可以考虑用适当的 <br>
替换换行符。标签。但是,这会失去一些带有空格的格式。
<?php
$data = array(
'foo' => array(
'bar',
'baz'
)
);
$jsonData = json_encode($data, JSON_PRETTY_PRINT);
echo "<h1>Original</h1>";
echo $jsonData;
echo "<h1><pre></h1><br>";
echo "<pre>" . $jsonData . "</pre>";
echo "<h1>str_replace()</h1><br>";
echo str_replace("\n", "<br>", $jsonData);
?>
关于php - 如何用 PHP 打印漂亮的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535909/
我是一名优秀的程序员,十分优秀!