2013-08-01 51 views
-2

與真正的選項,它工作正常。php json_decode包含數據[]

對不起,謝謝你們。

=============================================

我編碼PHP數組到JSON與此代碼

$rows = array(); 
if ($result = $mysqli->query($query)) { 
    while ($row = $result->fetch_assoc()) { 
     $rows[] = $row; 
    } 
    echo json_encode($rows); 
    /* free result set */ 
    $result->free(); 
} 

端解碼與

$array = json_decode($server_output) 

的$ server_output是這樣

[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}] 
[{"userid":"153795","userinfor":"xxxxxxxxx","userlocation":"CA"}] 
[{"userid":"131878","userinfor":"xxxxxxxxx","userlocation":"CA"}] 

但是,$數組爲null :(

由於前請先,

+0

你試圖改變你的數組名?嘗試添加json_decode($ server_output,true); – Darick

+0

你試過'$ array = json_decode($ server_output,true);'? – ops

+0

如何以及在哪裏爲'$ server_output'賦值?您是否啓用了錯誤報告('ini_set('display_errors','On'); error_reporting(E_ALL);'),並且是否有任何報告的錯誤?另外,你給出的例子是不正確的JSON。這是三個獨立的數組,而不是一個JSON對象 – Phil

回答

0

這不是有效的JSON在一起:

[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}] 
[{"userid":"153795","userinfor":"xxxxxxxxx","userlocation":"CA"}] 
[{"userid":"131878","userinfor":"xxxxxxxxx","userlocation":"CA"}] 

雖然,這部分是有效的:

[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}] 

或它們中的任何一個分開

$json = '[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}]'; 
print_r(json_decode($json, true)); 

輸出:

Array 
(
    [0] => Array 
     (
      [userid] => 96679 
      [userinfor] => xxxxxxxxx 
      [userlocation] => CA 
     ) 

) 

有效的格式對所有數據是這樣的:

[ 
    { 
     "userid": "96679", 
     "userinfor": "xxxxxxxxx", 
     "userlocation": "CA" 
    }, 
    { 
     "userid": "153795", 
     "userinfor": "xxxxxxxxx", 
     "userlocation": "CA" 
    }, 
    { 
     "userid": "131878", 
     "userinfor": "xxxxxxxxx", 
     "userlocation": "CA" 
    } 
]