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に自然に表示されない改行文字が含まれているようです。改行文字が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 ではないため、json_decode を壊します。

JSON での改行のエスケープに関する前の質問

改行を維持するためのまぶたのない回答を確認してください。短いバージョンは、それらをエスケープする必要があることです。たとえば、次のとおりです。

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

<br>または、ブラウザーでレンダリングするために改行をエスケープする代わりに、改行を に置き換えることもできます。

あなたはGIGOを持っています。入力を制御しているかどうかはわかりませんが、制御している場合は、フロントエンドでjson_encodeを使用してエスケープする必要があります。これにより、改行が自動的にエスケープされます (したがって保持されます)。

于 2012-12-05T01:40:32.717 に答える