如何在方括号和大括号中添加换行符JSONPHP文件



我有以下代码,可以在json.php中生成数据:

<?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 ']';
?>

获得以下结果:

[{
"id_news": 712,
"url": "es/deportes/futbol/ecuador/ligapro/serie-a/712/marcos-caicedo-recuerda-su-paso-en-los-equipos-del-astillero",
"cover_page": "https://i.imgur.com/kg7RBqK.jpg",
"alt_img": "Marcos Caicedo",
"mini_title": "Marcos Caicedo recuerda su paso en los equipos del astillero",
"mini_description": "El jugador de Liga de Quito no olvida su paso por Emelec y Barcelona",
"date_post": "2020-12-18 03:21:57",
"confg_img": null,
"main_cover": "relevant_news"
},{
"id_news": 708,
"url": "es/deportes/futbol/internacional/fichajes/708/el-fichaje-que-pretendia-ldu-para-la-defensa-podria-caerse",
"cover_page": "https://i.imgur.com/MmETkch.png",
"alt_img": "Jugadores de LDU celebrando un gol",
"mini_title": "EL fichaje que pretendu00eda LDU para la defensa podru00eda caerse",
"mini_description": "LDU tiene un competidor por el fichaje del central",
"date_post": "2020-12-16 20:26:51",
"confg_img": null,
"main_cover": "relevant_news"
}]

但我需要能够在括号和每个括号的另一个换行符之间获得一个换行,就像这样:

[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipitnsuscipit recusandae consequuntur expedita et cumnreprehenderit molestiae ut ut quas totamnnostrum 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 quodnaut est molestiasnmolestias et officia quis nihilnitaque dolorem quia"
},
{
"userId": 10,
"id": 100,
"title": "at nam consequatur ea labore ea harum",
"body": "cupiditate quo est a modi nesciunt solutanipsa voluptas error itaque dicta innautem qui minus magnam et distinctio eumnaccusamus ratione error aut"
}
]

正如这个json数据输出所示,我应该在我的PHP json代码中使用什么更改来获得相同的结果。

HTML

如果您可能要在web浏览器中向用户显示生成的JSON,您可以简单地使用:echo "[<br>";echo "<br>]";来插入html换行符。

API/REST脚本

如果结果由网络浏览器以外的任何其他应用程序处理,请使用:

  • echo "[ n";echo "n ]";适用于基于Linux/Unix的系统(包括MacOS10+(上的程序
  • 适用于低于9版本的MacOS程序的echo "[ r";echo "r ]";
  • 基于Windows系统程序的echo "[ rn";echo "rn ]";

反斜杠是通用的";逃逸;字符,因此,如果您不确定将在哪个平台上运行脚本,或者希望输出是通用的,无论操作系统如何,请使用"\r\n"的windows格式,每个操作系统都可以使用它自己的神奇

我看不出你这么做的原因,但可能是这样的,在括号后面和逗号后面添加n(新行(和&nbsp;&nbsp;(两个空格(

header('Content-type: application/json; charset=utf-8');
echo '['."n&nbsp;&nbsp;";  // <<<< here 
$count = 0;
while ($stmt->fetch()) {
if( $count ) {
echo ','."n&nbsp;&nbsp;";  // <<<< and here 
}
echo json_encode($member, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
++$count;
}
echo "n".']'; // <<<< and here 

CCD_ 11和CCD_">

如果空间不起作用,请尝试使用s

header('Content-type: application/json; charset=utf-8');
echo '['."nss";  // <<<< here 
$count = 0;
while ($stmt->fetch()) {
if( $count ) {
echo ','."nss";  // <<<< and here 
}
echo json_encode($member, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
++$count;
}
echo "n".']'; // <<<< and here 

最新更新