如何在JSON PHP文件的方括号和大括号中添加换行符?
我有以下代码以json php生成数据:如何在JSON PHP文件的方括号和大括号中添加换行符?,php,json,Php,Json,我有以下代码以json php生成数据: <?php $stmt = $con->prepare("SELECT id_news, url, cover_page, alt_img,
<?php
$stmt = $con->prepare("SELECT
id_news,
url,
cover_page,
alt_img,
mini_title,
mini_description,
date_post,
confg_img,
main_cover
FROM news ORDER BY id_news DESC LIMIT 5");
$stmt->execute();
$member = array();
$stmt->bind_result(
$member['id_news_sport'],
$member['url'],
$member['cover_page'],
$member['alt_img'],
$member['mini_title'],
$member['mini_description'],
$member['date_post'],
$member['confg_img'],
$member['main_cover']
);
header('Content-type: application/json; charset=utf-8');
echo '[';
$count = 0;
while ($stmt->fetch()) {
if( $count ) {
echo ',';
}
echo json_encode($member, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
++$count;
}
echo ']';
?>
但我需要能够在支架和每个支架的另一个换行符之间获得一个换行符,如下所示:
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 10,
"id": 99,
"title": "temporibus sit alias delectus eligendi possimus magni",
"body": "quo deleniti praesentium dicta non quod\naut est molestias\nmolestias et officia quis nihil\nitaque dolorem quia"
},
{
"userId": 10,
"id": 100,
"title": "at nam consequatur ea labore ea harum",
"body": "cupiditate quo est a modi nesciunt soluta\nipsa voluptas error itaque dicta in\nautem qui minus magnam et distinctio eum\naccusamus ratione error aut"
}
]
正如这个json数据输出所示,我应该在我的PHP json代码中使用哪些更改来获得相同的结果。我看不出您这样做的原因,但可能是这样的,在括号后面和逗号后面添加
\n
(一个新行)和
(两个空格)
header('Content-type: application/json; charset=utf-8');
echo '['."\n "; // <<<< here
$count = 0;
while ($stmt->fetch()) {
if( $count ) {
echo ','."\n "; // <<<< and here
}
echo json_encode($member, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
++$count;
}
echo "\n".']'; // <<<< and here
我看不出你这样做的原因,但可能是这样的,在括号后面和逗号后面加上
\n
(新行)和
(两个空格)
header('Content-type: application/json; charset=utf-8');
echo '['."\n "; // <<<< here
$count = 0;
while ($stmt->fetch()) {
if( $count ) {
echo ','."\n "; // <<<< and here
}
echo json_encode($member, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
++$count;
}
echo "\n".']'; // <<<< and here
HTML
如果您可能要在web浏览器中向用户显示生成的JSON,只需使用:echo“[
”;和echo”
”
插入html换行符
API/REST脚本
如果结果由web浏览器以外的任何其他应用程序处理,请使用:
echo“[\n”
和echo”\n]代码>用于基于Linux/Unix系统(包括MacOS 10+)上的程序
echo“[\r”
和echo”\r]代码>适用于MacOS版本低于9的程序
echo“[\r\n”
和echo”\r\n]基于Windows的系统上的程序的代码>
反斜杠是通用的“转义”字符,因此,如果您不确定将在哪个平台上运行脚本,或者希望输出是通用的,而不考虑操作系统,请使用windows格式的“\r\n”,每个操作系统都将发挥其自身的魔力HTML
如果您可能要在web浏览器中向用户显示生成的JSON,只需使用:echo“[
”;和echo”
”
插入html换行符
API/REST脚本
如果结果由web浏览器以外的任何其他应用程序处理,请使用:
echo“[\n”
和echo”\n]代码>用于基于Linux/Unix系统(包括MacOS 10+)上的程序
echo“[\r”
和echo”\r]代码>适用于MacOS版本低于9的程序
echo“[\r\n”
和echo”\r\n]基于Windows的系统上的程序的代码>
反斜杠是通用的“转义”字符,因此,如果您不确定将在哪个平台上运行脚本,或者希望输出是通用的,而不考虑操作系统,请使用“\r\n”的windows格式,每个操作系统都将发挥其自身的魔力空格不起作用,朋友,我打印以下内容:{“id\u news”:713,…
然后尝试添加\s
而不是
空格不起作用,朋友,我打印以下内容:{“id\u news”:713,…
然后尝试添加\s
而不是
header('Content-type: application/json; charset=utf-8');
echo '['."\n\s\s"; // <<<< here
$count = 0;
while ($stmt->fetch()) {
if( $count ) {
echo ','."\n\s\s"; // <<<< and here
}
echo json_encode($member, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
++$count;
}
echo "\n".']'; // <<<< and here