代码之家  ›  专栏  ›  技术社区  ›  Asim

PHP JSON结构不正确

  •  -1
  • Asim  · 技术社区  · 8 年前

    我正在开发谷歌标签管理器API(v2)。我进行多个API调用,并创建一个对象,将其作为JSON发送到客户端。

    我在尝试使JSON对象看起来像我想要的一样时遇到了一些困难。

    我差一点就完成了,但还没完成。

    举例来说,这是我现在的JSON对象:

    {
        "Account2": {
            "0": {
                "accountId": "1746756959",
                "containerId": "7454209"
            },
            "1": 3,
            "2": 3,
            "3": 4,
            "4": {
                "accountId": "1746756959",
                "containerId": "7519183"
            },
            "5": 1,
            "6": 1,
            "7": 2,
            "not_live": "testcontainer"
        },
        "Account 1": [{
            "accountId": "1766143525",
            "containerId": "7483653"
        }, 2, 1, 1],
        "GTMdocx": [{
            "accountId": "2120037423",
            "containerId": "8031029"
        }, 0, 0, 1],
        "Account3": [{
            "accountId": "2128191242",
            "containerId": "8038449"
        }, 0, 0, 0]
    }
    

    如您所见,结构如下:
    1) 顶层:AccountName(Account2)
    2) 0:=容器级别
    3) 如你所见,1,2,3是一些数据。

    我的问题是我想要下面显示的那些数字 containerid 在同一物体的正下方。

    索引1、2和3应在“Account2:0:”下(在containerId下)。就像它现在显示为一个对象本身一样,这是错误的。

    这是我的PHP代码:

    static public function listAllContainers() {
        $containers[] = array();
        foreach (self::listAccounts()->account as $accountKey => $account) {
            foreach (self::listAccountsContainers($account["path"]) as $account_container) {
    
                try { //Because some containers might not have live-version
                    $container_version = self::listAccountsContainersVersion($account_container['path']);
                    $containers[$account['name']][] = $account_container;
                    $containers[$account['name']][] = count($container_version['tag']);
                    $containers[$account['name']][] = count($container_version['trigger']);
                    $containers[$account['name']][] = count($container_version['variable']);
    
                } catch (\Exception $e) {
                    $containers[$account['name']]['not_live'] = $account_container['name'];
                }
    
            }
        }
        $filtered_array = (object)array_filter((array)$containers); // Removes empty object
        return $filtered_array;
    }
    

    例如,这一行 $containers[$account['name']][] = count($container_version['tag']); 在JSON对象中显示为“1”,我希望它位于“account2:{0:..}”内。不可能写出它,因为它不能说明对象 我想要JSON:

    {
        "Account2": {
            "0": {
                "accountId": "1746756959",
                "containerId": "7454209",
                "tags": 3,
                "triggers": 3,
                "variables": 4
            },
            "4": {
                "accountId": "1746756959",
                "containerId": "7519183",
                "tags": 1,
                "triggers": 1,
                "variables": 2,
                "not_live": "testcontainer"
    
        (....)
    

    我已经拨打了 "Tags, Triggers, Variables" .

    1 回复  |  直到 8 年前
        1
  •  1
  •   Jeff    8 年前

    将其添加到 $account_container 第一

    $account_container->tag = count($container_version['tag']);
    $account_container->trigger = count($container_version['trigger']);
    $account_container->variable = count($container_version['variable']);
    
    // then add the whole account to the main container
    $containers[$account['name']][] = $account_container;