这是我的代码:
var_dump(json_decode($data['event']->options['meals']['options'][0]['option'], true));
echo '<br />';echo '<br />';
var_dump($data['event']->options['meals']['options'][0]['option']);
echo '<br />';echo '<br />';
var_dump(json_decode('[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]', true));
这是我的输出:
NULL
string(279) "[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]"
array(2) { [0]=> array(2) { ["name"]=> string(16) "Petit Tenderloin" ["description"]=> string(115) "Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. " } [1]=> array(2) { ["name"]=> string(16) "Chicken Piccatta" ["description"]=> string(72) "In lemon caper sauce, served with a timbal of wild rice and vegetables. " } }
为什么当我输入字符串文字时,我得到了正确的数组,但是当我传入一个变量时,我得到了 NULL?我觉得我错过了一些超级简单的东西......
编辑:找出原因看起来变量有一个换行符,自然不会显示在 HTML 中。看起来新的行字符中断json_decode...
除了删除新行之外,有谁知道解决方法吗?(如果可以的话,我宁愿把它们留在里面)
确保数组在var_dump
其内容的第一行上有数据。 我无法重现您的错误。
我的代码:
<?php
$data['event']->options['meals']['options'][0]['option'] = '[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]';
var_dump(json_decode($data['event']->options['meals']['options'][0]['option'], true));
echo '<br />';echo '<br />';
var_dump($data['event']->options['meals']['options'][0]['option']);
echo '<br />';echo '<br />';
var_dump(json_decode('[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]', true));
?>
这是它为我产生的输出:
array(2) {
[0] =>
array(2) {
'name' =>
string(16) "Petit Tenderloin"
'description' =>
string(115) "Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "
}
[1] =>
array(2) {
'name' =>
string(16) "Chicken Piccatta"
'description' =>
string(72) "In lemon caper sauce, served with a timbal of wild rice and vegetables. "
}
}
<br /><br />
string(278) "[{"name":"Petit Tenderloin","description":"Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "},{"name":"Chicken Piccatta","description":"In lemon caper sauce, served with a timbal of wild rice and vegetables. "}]"
<br /><br />
array(2) {
[0] =>
array(2) {
'name' =>
string(16) "Petit Tenderloin"
'description' =>
string(115) "Wrapped in Apple Wood Bacon, borsoun whipped mashed potatoes, roasted baby vegetable, with sun dried cherry sauce. "
}
[1] =>
array(2) {
'name' =>
string(16) "Chicken Piccatta"
'description' =>
string(72) "In lemon caper sauce, served with a timbal of wild rice and vegetables. "
}
}
未转义的新行字符将中断json_decode,因为这不是有效的 JSON。
上一个关于 JSON 中转义换行符的问题。
查看无眼睑的答案,以保持换行符。 简短的版本是您需要转义它们,例如:
$text = str_replace("n", "\n", $text);
或者,您可能希望将换行符替换为 <br>
,转义换行符以便在浏览器中呈现。
你有GIGO。 不确定您是否在控制输入,但如果是,那么您应该使用 json_encode 在前端对其进行转义,这将自动转义(从而保留)换行符。