php json_encode fails without error

时间:2015-10-29 16:00:04

标签: php json

PHP Version: 5.6.15RC1
Compiler: MSVC11 (Visual C++ 2012)
Architecture:x86

I am having problem json_encode ing a multi dimensional php array. The main problem is that no error is generated (json_last_error=0).

The array is indexed by a string and testing each of these indexes seperately has been done within the array compilation has been done on the outer indexes by using:

$test = json_encode($account[$q_id]);
if (strlen($test) < 2) {
    $error = json_last_error();
}

Stepping through several showed correct json output. Then a breakpoint in the if statement was placed to identify any encoding issues, however it never stopped on the $error... line.

Database connection:

$connection_cfg = array("Database" => $db["database"], "CharacterSet" =>     "UTF-8", "UID" => $db["uname"], "PWD" => $db["pword"], "ReturnDatesAsStrings" =>true);
$this->connection = sqlsrv_connect($db["host"], $connection_cfg);

I am stuck on how to proceed to debug this.

2 个答案:

答案 0 :(得分:0)

空的JSON字符串如下所示:

[]

这是2个字符。 您的错误检测将无法正常工作,因为它会检查1个或更少的字符。

更改

if (strlen($test) < 2) {
    $error = json_last_error();
}

if (strlen($test) == 2) {
    $error = json_last_error();
}

您的错误检测应该有效。

如果您无法解决JSON错误中指出的问题,请通过错误更新我们。

答案 1 :(得分:-1)

经验教训,在调试工具(netbeans watch等)中总会有一些不信任。 我在看到一个随机的&#39; {&#39;作为输出(不知道为什么netbeans像这样显示它)。 这是javascript被打破的行,如

var a=<?php echo $thejsonstuff ?>

我首先尝试从netbeans结果进行php调试。