【问题标题】:Complex PHP Object to JSon String复杂的 PHP 对象到 JSON 字符串
【发布时间】:2014-04-25 19:44:00
【问题描述】:

我在PHP 中有一个复杂对象,需要对其进行解析以构建 Json 字符串。我发现了很多我在这里和其他网站找到的例子,但没有一个工作。另一个问题是我的主机在 PHP 5.2 上运行,我无法升级。

这是我的var_dump($myObj)的一个例子:

object(Park)[4]
private 'idObj' => string '60304' (length=5)
private 'name' => string 'AlphaSurf' (length=9)
private 'address' => 
object(Address)[6]
  private 'idObj' => string '40304' (length=5)
  private 'street' => string 'Champ de la Vigne' (length=17)
  private 'number' => string '7' (length=1)
  private 'zip' => string '1470' (length=4)
  private 'city' => string 'Estavayer-le-Lac' (length=16)
  private 'country' => 
    object(Country)[8]
      private 'idObj' => string '30039' (length=5)
      private 'name' => string 'Switzerland' (length=11)
      private 'flag' => string 'switzerland.gif' (length=15)
  private 'usState' => null
private 'contactInfo' => 
object(ContactInfo)[7]
  private 'idObj' => string '70304' (length=5)
  private 'phone' => string '' (length=0)
  private 'email' => string '' (length=0)
  private 'emailcode' => null
  private 'confirmed' => string '1' (length=1)
  private 'website' => string 'www.alphasurf.ch' (length=16)
  private 'mobile' => string '' (length=0)
  private 'fax' => string '' (length=0)
  private 'newsletter' => string '0' (length=1)
private 'owner' => 
object(User)[9]
  private 'idObj' => string '50001' (length=5)
  private 'username' => string 'emaborsa' (length=8)
  private 'password' => string '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8' (length=40)
  private 'type' => string 'ADMIN' (length=5)
  private 'state' => string 'ACTIVE' (length=6)
  private 'ip' => string '' (length=0)
  private 'time' => string '0' (length=1)
  private 'address' => null
  private 'contactInfo' => 
    object(ContactInfo)[11]
      private 'idObj' => string '1' (length=1)
      private 'phone' => null
      private 'email' => string 'info@emaborsa.com' (length=17)
      private 'emailcode' => null
      private 'confirmed' => string '1' (length=1)
      private 'website' => null
      private 'mobile' => null
      private 'fax' => null
      private 'newsletter' => string '1' (length=1)
private 'logo' => string 'Champ de la Vigne 71470' (length=23)
private 'xcoord' => string '46856912' (length=8)
private 'ycoord' => string '6846918' (length=7)
private 'state' => string 'HIDDEN' (length=6)
private 'detail' => 
object(ParkDetail)[10]
  private 'idObj' => string '1' (length=1)
  private 'descriptionIT' => string '' (length=0)
  private 'descriptionEN' => string '' (length=0)
  private 'descriptionDE' => string 'xcxcx' (length=5)
  private 'type' => string '' (length=0)
  private 'kickers' => string '0' (length=1)
  private 'boxes' => string '0' (length=1)
  private 'rails' => string '0' (length=1)
  private 'specials' => string '0' (length=1)
  private 'specialsDescriptionIT' => null
  private 'specialsDescriptionEN' => null
  private 'specialsDescriptionDE' => null
  private 'dimension' => string '0' (length=1)
private 'lastPayment' => null

所有属性都是私有的,但也有公共的 getter 和 setter。

【问题讨论】:

  • 使用json_encode有什么问题?
  • 所有属性都是私有的?你打算用 json 做什么?
  • 你可以尝试序列化/反序列化函数。
  • 你可以给你的类一个 toJson 方法,你可以在其中创建一个由你的私有变量和 njson_encode 组成的数组并返回它?
  • json_encode($myObj) 返回{}

标签: php object json


【解决方案1】:

我认为您需要公开私有属性值是一个设计错误。

当然,在某些情况下应该这样做。 正如PHP Object To JSON format 所指出的那样,一种方法是通过反射。

这是一个使用 php ReflectionClass 来实现你想要的简单示例:

function getJson($object)
{
    $result = array();
    $refl = new ReflectionClass($object);
    foreach ($refl->getProperties() as $prop) {
        $prop->setAccessible(true);
        $result[$prop->name] = $prop->getValue($object);
    }
    return json_encode($result);
}

【讨论】:

    【解决方案2】:

    试试这个

    public function encodeJSON() 
    {    
        foreach ($this as $key => $value) 
        { 
             if($value instanceOf(stdClass)){
                 $json->$key = $value->encodeJSON();
             }else
                 $json->$key = $value; 
        } 
        return json_encode($json);
    }
    

    我正在尝试将私有成员移动到可以由普通json_encode() 写入的新对象,并且在第 6 行中,如果它不是原始类型,我将递归调用它的 foreach 参数

    【讨论】:

    • ...好吧,我必须把它放在哪里?
    • 到你想要回显它的 json 的类中,使用 $this->encodeJSON() 并且你必须将它写入每个具有你想要 JSON 的私有成员的对象中
    • 如果我用这个方法创建一个 Json 类并从这个新的 Json 类扩展每个类,它是否有效?
    • 您是在告诉我阅读有关封装的内容吗?您的属性是私有的,因此如果您将此方法放在一个类中并扩展它,它将无法访问私有属性。
    • @MajedDH 如果您引用您粘贴的原始答案会很好。
    【解决方案3】:

    当您运行 PHP toArray 方法,返回一个包含所有属性的数组(不管它们是公共的、私有的还是受保护的)。

    例如:

    public class Park {
    
        private $idObj;
        private $address;
    
        public function toArray() {
    
            $toArray = array(
                'idObj' => $this->idObj,
                'address' => $this->address->toArray() // Assuming address is an Address object
            );
        }
    }
    

    在你所有的类和子类中这样做,然后你就可以使用了:

    $park = new Park(/* your values to initialize the object */);
    echo json_encode($park->toArray());
    

    【讨论】:

    • 你确定这是最好的方法吗?我有 25 节课,我必须在每节课上做。我想一定有一种方法可以通过在一个类或其他东西中的反射来做到这一点。
    • 嗯,随着 PHP 5.4 的出现,JsonSerializable 接口可能会有所帮助,但对于早期版本的 PHP,实现这一点要“复杂”得多。我建议你看看这个答案:stackoverflow.com/a/4697671/831669 和这个:stackoverflow.com/a/14796817/831669
    • 我已经看过这样的例子,这意味着我必须为每个类编写一个适当的方法......
    • @Emaborsa 或升级您过时的 PHP 版本。
    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多