简体   繁体   English

JSON响应返回null

[英]JSON response returning null

I have followed a tutorial from here http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ but I have come to a problem with the JSON response. 我从这里开始跟随了一个教程http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/,但是JSON响应出现了问题。 It is returning null. 它返回null。 I think it is due to the character encoding as some of the content that is being returned as null includes ° symbols. 我认为这是由于字符编码所致,因为返回的某些内容为null包含°符号。

The PHP code is: 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;

and the response is "ntmContent":null} ntmContent is what contains the odd characters but they appear fine in the database. 响应是“ ntmContent”:null} ntmContent是包含奇数字符的内容,但它们在数据库中看起来很好。

The tutorial doesn't cover issues surrounding character encoding so doesn't prepare for it but how should the $response be handled to accept the odd characters? 本教程没有涉及字符编码的问题,因此没有为此做准备,但是应该如何处理$ response以接受奇数字符?

Thanks 谢谢

When json_encode fails to encode a value, it outputs null. json_encode无法对值进行编码时,它将输出null。

This is due to the odd characters you made reference too in your post. 这是由于您在帖子中也引用了奇怪的字符。

json_encode() only works with UTF-8 . json_encode()仅适用于UTF-8

Try like this: 尝试这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM