这是我的代码:
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中。看起来新行char会破坏json_decode ...
除了删除新线以外,还有其他人知道吗? (如果可以,我宁愿让他们进来)
答案 0 :(得分:1)
确保数组在第一行有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));
&GT;
这是它为我制作的输出:
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. "
}
}
答案 1 :(得分:0)
未转义的新行字符会破坏json_decode,因为它不是有效的JSON。
Previous question on escaping line breaks in JSON。
检查眼睑无法回答的问题,以保持换行符。简短版本就是你需要逃避它们,例如:
$text = str_replace("\n", "\\n", $text);
或者,您可能希望将换行符替换为<br>
,转义它们以便在浏览器中进行渲染。
你有GIGO。不确定你是否正在控制输入,但如果你是,那么你应该使用json_encode在前端转义它,它将自动转义(并因此保留)换行符。