繁体   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