使用JSON动态添加元素

时间:2014-08-25 21:48:53

标签: php arrays json object serialization

我想用PHP和JsonSerializable接口动态地将按钮添加到我的JSON字符串。

为了测试,我有一个带有“apps”表的数据库,其中包含“id”,“token”和“json”列。

View元素包含Buttons元素,Buttons元素包含Toto元素。

我有一个每个元素的类,我将所有类属性序列化为JSON。

我创建了一个小形式来添加按钮,按钮被附加但不是JSON中的同一级别。

这是我的代码:

<?php

header('Content-Type: text/html; charset=utf-8');
require_once('init.php'); // Database connection

class View implements JsonSerializable
{
    private $name;
    private $id;
    private $Button;

    function __construct($name, $id, $buttons)
    {

        $this->name = $name;
        $this->id = $id;
        $this->Button = array($buttons);
    }

    public function setButton($buttons) { array_push($this->Button, $buttons); }

    public function jsonSerialize() { return (get_object_vars($this)); }
}

class Toto implements JsonSerializable
{
    private $name;

    function __construct($name)
    {
        $this->name = $name;
    }

    public function jsonSerialize() { return (get_object_vars($this)); }
}

class Button implements JsonSerializable
{
    private $name;
    private $id;
    private $Element;

    function __construct($name, $id)
    {
        $this->name = $name;
        $this->id = $id;
        $this->Element = new Toto('Titi');
    }

    public function jsonSerialize() { return (get_object_vars($this)); }
}

?>

<form action="" method="post">
    <?php
    $q = $mysqli->query('
         SELECT json
         FROM apps
         WHERE token = "abc"');
    if ($q->num_rows >= 1)
    {
        $d = $q->fetch_object();
        $result = $d->json;
        var_dump(json_decode($result)->Button);
    }
    else
        $result = null;
    if (isset($_POST['add']))
    {
        if ($q->num_rows >= 1)
        {
            $view = new View('Home', 1, json_decode($result)->Button);
            $view->setButton(new Button($_POST['name'], 12));
            $newJSON = json_encode($view, (JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));

            $mysqli->query('
            UPDATE apps
            SET token = "abc",
                json = "' . $mysqli->real_escape_string($newJSON) . '"
            WHERE token = "abc"');
        }
        else
        {
            $view = new View('Home', 1, new Button($_POST['name'], 12));
            $newJSON = json_encode($view, (JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));

            $mysqli->query('
            INSERT INTO apps
            SET token = "abc",
                json = "' . $mysqli->real_escape_string($newJSON) . '"');
        }

        header('Location: archi.php');
        exit;
    }
    ?>
    <pre><?php print_r($result); ?></pre>
    <input type="text" name="name" placeholder="Name" />
    <button type="submit" name="add">Add button</button>
</form>

这里我添加了三个按钮的当前输出:

{
"name": "Home",
"id": 1,
"Button": [
    [
        [
            {
                "name": "Send",
                "id": 12,
                "Element": {
                    "name": "Titi"
                }
            }
        ],
        {
            "name": "Cancel",
            "id": 12,
            "Element": {
                "name": "Titi"
            }
        }
    ],
    {
        "name": "Invite",
        "id": 12,
        "Element": {
            "name": "Titi"
        }
    }
]
}

我想要这个结果:

{
"name": "Home",
"id": 1,
"Button": [
    {
        "name": "Send",
        "id": 12,
        "Element": {
            "name": "Titi"
        }
    },
    {
        "name": "Cancel",
        "id": 12,
        "Element": {
            "name": "Titi"
        }
    },
    {
        "name": "Invite",
        "id": 12,
        "Element": {
            "name": "Titi"
        }
    }
]
}

我怎样才能达到这个结果?我被卡住了。

Mickey42。

0 个答案:

没有答案