如何允许 json_encode() 接受 \n 作为新行?

0 php json newline

我正在将数组编码为 JSON 格式,但它不会从换行符 (\n) 中换行。

那么如何实现这一点

$array = ["string" => "hello \ngood morning!!"];

$encodedJson = json_encode($array);
dd(encodedJson);
Run Code Online (Sandbox Code Playgroud)

输出

{"string":"hello \ngood morning!!"}
Run Code Online (Sandbox Code Playgroud)

但我想要的是

{
  "string" :"hello
   good morning!!"
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过

json_encode($array,JSON_UNESCAPED_LINE_TERMINATORS);

Que*_*tin 6

您想要的不是有效的 JSON。

JSON 数据格式不支持字符串中间的文字换行。

没有办法生成json_encode该输出(即使可以,也无法在任何标准 JSON 解析器中解析结果)。