0

这是我的代码:

    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 ...

除了删除新行之外,任何人都知道解决这个问题的方法吗?(如果可以的话,我宁愿把它们留在里面)

4

2 回答 2

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));

?>

这是它为我产生的输出:

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. "
  }
}
于 2012-12-05T00:52:12.600 回答
0

未转义的换行符将破坏 json_decode,因为这不是有效的 JSON。

上一个关于在 JSON 中转义换行符的问题

在那里查看 eyeliidlessness 的答案以保持换行符。简短的版本是您需要转义它们,例如:

$text = str_replace("\n", "\\n", $text);

或者,您可能希望将换行符替换为<br>, insetad 以转义它们以在浏览器中呈现。

你有GIGO。不确定您是否正在控制输入,但如果是,那么您应该在前端使用json_encode对其进行转义,这将自动转义(并因此保留)换行符。

于 2012-12-05T01:40:32.717 回答