簡體   English   中英

JSON響應返回null

[英]JSON response returning null

我從這里開始跟隨了一個教程http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/,但是JSON響應出現了問題。 它返回null。 我認為這是由於字符編碼所致,因為返回的某些內容為null包含°符號。

PHP代碼為:

// Check for empty result
if (mysqli_num_rows($result) > 0) {
// Looping through all results

$response["ntmNotices"] = array();

while ($row = mysqli_fetch_array($result)) {

    $ntmRow = array();
    $ntmRow["uploadDate"] = $row["uploadDate"];
    $ntmRow["uploadTime"] = $row["uploadTime"];
    $ntmRow["ntmTitle"] = $row["ntmTitle"];
    $ntmRow["ntmDate"] = $row["ntmDate"];
    $ntmRow["ntmContent"] = $row["ntmContent"];

    // push single row into final response array
    array_push($response["ntmNotices"], $ntmRow);
}
// success
$response["success"] = 1;

// echoing JSON response    
echo json_encode($response);

} else {
// no rowsfound
$response["success"] = 0;

響應是“ ntmContent”:null} ntmContent是包含奇數字符的內容,但它們在數據庫中看起來很好。

本教程沒有涉及字符編碼的問題,因此沒有為此做准備,但是應該如何處理$ response以接受奇數字符?

謝謝

json_encode無法對值進行編碼時,它將輸出null。

這是由於您在帖子中也引用了奇怪的字符。

json_encode()僅適用於UTF-8

嘗試這樣:

$ntmRow["ntmContent"] = utf8_encode($row["ntmContent"]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM