【问题标题】:JSON string to PHP array NULLJSON字符串到PHP数组NULL
【发布时间】:2012-12-05 00:39:34
【问题描述】:

这是我的代码:

    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 之后,您可以尝试使用 json_last_error 来尝试确定您为什么会得到 NULL。我的猜测是你正在进行一些编码,当你执行 var_dump 时会被破坏。

标签: php multidimensional-array json


【解决方案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. "
  }
}

【讨论】:

  • 您可以告诉它有数据,因为在第 3 行不更改代码它输出正确的字符串...这就是为什么我很困惑。传入变量时它失败了,但是当我传入字符串文字时,它成功了......
  • 如果我将字符串分配给 $x 并在 $x 上执行 json_decode,我会得到正确的详细信息......所以看起来它可能与该变量有关......跨度>
【解决方案2】:

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

Previous question on escaping line breaks in JSON。

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

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

或者,您可能希望用&lt;br&gt; 替换换行符,以便将它们转义以在浏览器中呈现。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多