PHP json_encode返回空结构 [英] PHP json_encode returning empty sctructure

查看:514
本文介绍了PHP json_encode返回空结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用json_encode将对象数组发送回用户,并且在我代码的其他部分它可以正常工作,但是在此一处它返回一个空的结构字符串.

I'm calling json_encode to sendo a objects array back to the user, and at other parts of my code it works properly, but at this one it return a empty structure string.

这是在编码之前我在数组上调用json_encode的结果:

Here is the result when I call json_encode on the array before encoding:

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之后的结果:

Here is the result after json_encode:

[{},{},{}]

可以注意到,原始数组中没有特殊字符,因此我认为这不是编码问题. 这是我称之为json_encode的代码:

As can be noticed, there is no special characters in the original array, so I do not think it is a encoding issue. Here is the code where I call 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;
}

推荐答案

您具有受保护的私有属性,因此无法使用任何函数(包括json_encode())直接访问对象的属性.

You have protected and private attributes, so you can not use any functions, including json_encode() to access directly to your object's property.

使用 JSONSerialize

这篇关于PHP json_encode返回空结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆