【发布时间】:2016-02-24 21:30:18
【问题描述】:
我正在调用 json_encode 将一个对象数组发送回用户,在我的代码的其他部分它可以正常工作,但在这部分它返回一个空结构字符串。
这是我在编码之前对数组调用 json_encode 时的结果:
array(3) {
[0]=>
object(MinaProxy)#5 (7) {
["carregado":"MinaProxy":private]=>
bool(true)
["link":"MinaProxy":private]=>
int(1)
["idMina":protected]=>
int(1)
["qntOuro":protected]=>
int(2000)
["x":protected]=>
int(307)
["idPartida":protected]=>
int(1)
["proximo":protected]=>
int(1)
}
[1]=>
object(MinaProxy)#6 (7) {
["carregado":"MinaProxy":private]=>
bool(true)
["link":"MinaProxy":private]=>
int(2)
["idMina":protected]=>
int(2)
["qntOuro":protected]=>
int(2000)
["x":protected]=>
int(512)
["idPartida":protected]=>
int(1)
["proximo":protected]=>
int(2)
}
[2]=>
object(MinaProxy)#7 (7) {
["carregado":"MinaProxy":private]=>
bool(true)
["link":"MinaProxy":private]=>
int(3)
["idMina":protected]=>
int(3)
["qntOuro":protected]=>
int(2000)
["x":protected]=>
int(716)
["idPartida":protected]=>
int(1)
["proximo":protected]=>
NULL
}
}
这是json_encode之后的结果:
[{},{},{}]
可以注意到,原始数组中没有特殊字符,所以我认为这不是编码问题。 这是我调用 json_encode 的代码:
elseif($dados['acao'] == 'recuperarMinas') {
$i = 0;
$array = array();
while ($this->partida->minas->temProximo()) {
$array[$i] = $this->partida->minas->getProximoAvancando();
$array[$i]->getIdPartida();
$i++;
}
$_SESSION['partida'] = $this->partida;
$retornoJson = json_encode($array);
return $retornoJson;
}
【问题讨论】: