JSON を使用してオブジェクトをエンコードし、AJAX 要求に応答したいと考えています。まず、オブジェクトを配列に変換し (この結果は問題ないように見えます)、次にjson_encode
配列を JSON 形式にエンコードしますが、予期しない結果が得られます。取得した JSON 文字列は、プロパティ名の前にクラス名があり、null 文字「\0000」が多くの場所に表示されます。ファイルはすべて UTF-8 でエンコードされています。を使用するget_object_vars
と、結果が得られarray = []
ます。
この問題を解決するにはどうすればよいですか?
私は結果を得ました:{"\u0000DB\u0000connection":null,"\u0000DB\u0000serverName":"localhost","\u0000DB\u0000userName":"root","\u0000DB\u0000password":null,"\u0000DB\u0000dbName":"thahtin"}
使用したコードは次のとおりです。
class DB
{
private $connection;
private $serverName;
private $userName;
private $password;
private $dbName;
public function __construct()
{
$config = new Configuration();
$this->serverName = 'localhost'; //$config->getConfig("server");
$this->userName = 'root'; //$config->getConfig("userName");
$this->password = null; //$config->getConfig("password");
$this->dbName = 'thahtin'; //$config->getConfig("database");
}
public function open()
{
if(!$this->connection)
mysql_close($this->connection);
$this->connection = mysql_connect($this->serverName, $this->userName, $this->password);
if(!$this->connection)
{
die('Could not connect. Error: ' . mysql_error());
}
mysql_select_db($dbName);
}
}
$db = new DB();
echo json_encode((array)$db);