几个级别的PHP类到JSON

时间:2014-01-14 09:56:17

标签: php json object

我有一个PHP类,我想在几个级别的JSON中进行转换,比如这种类型:

{"interface":{"Version":"0"},"Container":[{"id":"1","Element":[{"text":"Test","id":"0"},{"text":"Toto","id":"1"}]}]}

在我的PHP类中,我有一个函数,它返回属于数组的私有属性的JSON:

return (json_encode((get_object_vars($this)), JSON_UNESCAPED_UNICODE));

我班级的私人属性:

private $interface = '';
private $Container = array(array('id' => '1'));
private $Element = array('text' => 'Test', 'id' => '0');

你知道我怎么能拥有像上面这样的JSON吗?

很高兴读到你。

4 个答案:

答案 0 :(得分:0)

不确定您的类成员,但只要它们可访问,您就可以生成JSON字符串。下面是这个例子

    $Contenant = array(array('id' => '1'));
    $Element = array('text' => 'Test', 'id' => '0');

    $json = json_encode(array('inerrface'=>array(
                        'content'=>$Contenant,
                        "Element"=>$Element
                          )     
                       )
                );
    echo $json ;

答案 1 :(得分:0)

您可以像以下示例中那样实现IteratorAggregate接口

class YourClass implements IteratorAggregate {
    protected $member1 = array();
    protected $member2 = array();

    ...

    public function getIterator() {
        $tmpArr = array();

        // create the structure you want in $tmpArr

        return new ArrayIterator($tmpArr);
    }
}

$myClass = new MyClass();
$iterator = $myClass->getIterator();

$encodedData = json_encode($iterator);

从PHP5.4开始,您就可以使用JsonSerializable接口了。使用此界面,您可以使用直接修改,例如:http://de2.php.net/manual/en/jsonserializable.jsonserialize.php

中给出的示例

玩得开心! ;)

答案 2 :(得分:0)

这将帮助您入门,看一下构造函数所需的数据输入。您可以使私有函数执行相同的操作,将值分配给结构然后打印它:

class Test{

    private $data;

    public function __construct($version = 0, $records = array()){

        $data['interface'] = array('Version' => $version);
        $data['Container'] = array();

        for ($i = 0; $i < count($records); $i++) {
            $data['Container'][$i] = $records[$i];
        }

        // your test input
        print_r(json_decode('{"interface":{"Version":"0"},"Container":[{"id":"1","Element":[{"text":"Test","id":"0"},{"text":"Toto","id":"1"}]}]}',true));
        // actual input
        print_r($data);
        // printing our actual data as json string
        echo json_encode($data);
    }

    public function __destruct(){

    }

}

$element1 = array('text' => 'Test', 'id' => 0);
$element2 = array('text' => 'Toto', 'id' => 1);
$elements = array($element1, $element2);
$record = array('id' => 1, 'element' => $elements);
$records = array($record);

new Test(0, $records);

答案 3 :(得分:-1)

你需要构建它,工作示例:example

   class test {
    private $interface = '';
    private $Contenant = array(array('id' => '1'));
   //private $Element = array(array('text' => 'Test', 'id' => '0'));

    public function json(){

        $this->Contenant = array(
            array('id' => '1',
                 'Element' =>  array(array('text' => 'Test', 'id' => '0'))
                ),

        );

        return json_encode((get_object_vars($this)), JSON_UNESCAPED_UNICODE);
    }
}


$t = new test();
$encode = $t->json();

echo $encode;

class test { private $interface = ''; private $Contenant = array(array('id' => '1')); //private $Element = array(array('text' => 'Test', 'id' => '0')); public function json(){ $this->Contenant = array( array('id' => '1', 'Element' => array(array('text' => 'Test', 'id' => '0')) ), ); return json_encode((get_object_vars($this)), JSON_UNESCAPED_UNICODE); } } $t = new test(); $encode = $t->json(); echo $encode;

OUTPUT

{"interface":"","Contenant":[{"id":"1","Element":[{"text":"Test","id":"0"}]}]}