2012-12-05 32 views
0

這裏是我的代碼:JSON字符串PHP數組NULL

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

任何人都知道的周圍其他的方式不是刪除新線? (我寧願讓他們在,如果我能)

+0

不知道,但你的第一個之後的var_dump,你可以嘗試使用[json_last_error(http://php.net/manual/en/function.json-last-error.php),嘗試確定爲什麼你獲得NULL。我的猜測是你有一些編碼正在進行,當你執行var_dump時會被編碼。 – ernie

回答

0

的轉義的新行字符將打破json_decode因爲這不是有效的JSON 。

Previous question on escaping line breaks in JSON

出現退房eyelidlessness'答案保持換行符。短版是你需要逃避他們,例如:

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

或者,您可能需要使用<br>取代換行符,逃避他們在瀏覽器中渲染的insetad。

你有GIGO。不知道你是否在控制輸入,但如果你是,那麼你應該在前端使用json_encode來逃避它,它會自動逃脫(並因此保留)換行符。

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. " 
    } 
} 
+0

你可以告訴它有數據,因爲在第三行沒有改變代碼輸出正確的字符串...這就是爲什麼我很困惑。當傳遞變量時,它失敗了,但是當我傳入字符串時,它會成功... –

+0

如果我將字符串賦值給$ x並在$ x上執行json_decode,我會得到適當的詳細信息...因此,看起來它可能與這個變量有關... –

+0

dang it我現在越來越餓了... –