【问题标题】:How to decode Google Tag Manager JSON如何解码谷歌标签管理器 JSON
【发布时间】:2019-11-18 08:56:23
【问题描述】:

我正在使用 Google Tag Manager 库来获取我帐户中的所有标签。我正在从https://www.googleapis.com/discovery/v1/apis/tagmanager/v2/rest 获取 JSON 数据。此 JSON 不适用于我,我无法使用 PHP 对其进行解码。

例子:

{ 
   "tag":[ 
      { 
         "path":"tags/1",
         "accountId":"1"
      },
      { 
         "path":"tags/2",
         "accountId":"1"
      }
   ],
   "tag":[ 
      { 
         "path":"tags/1",
         "accountId":"2"
      },
      { 
         "path":"tags/2",
         "accountId":"2"
      }
   ],
   "tag":[ 
      { 
         "path":"tags/1",
         "accountId":"3"
      }
   ]
}

并在json debugger website 上进行测试我有错误Duplicate key, names should be unique.

如何修复此 JSON 以使其与 PHP json_decode($json, true) 一起使用?

以前的丑陋代码

$json = str_replace('\n', '', $get_json);
$json = preg_replace('/\s+/', ' ', $json);
$json = str_replace('}{}{', ',', $json);
$json = str_replace('}{', ',', $json);
$json = str_replace('"tag": [', '"tag": ', $json);
$json = str_replace('} } ]', '} } }', $json);
$json = str_replace('tag":', 'tag_manage":', $json);
$json = str_replace('tag_manage', 'tag_manage', $json);
$json = preg_replace('/tag_manage/', 'tag_1_manage', $json, 1);
$json = preg_replace('/tag_manage/', 'tag_2_manage', $json, 1);
$json = preg_replace('/tag_manage/', 'tag_3_manage', $json, 1);

【问题讨论】:

标签: php json google-tag-manager


【解决方案1】:

您有多个名为tag 的对象的属性。类似的东西

{ 
   {"tag":[ 
      { 
         "path":"tags/1",
         "accountId":"1"
      },
      { 
         "path":"tags/2",
         "accountId":"1"
      }
   ]},
   {"tag":[ 
      { 
         "path":"tags/1",
         "accountId":"2"
      },
      { 
         "path":"tags/2",
         "accountId":"2"
      }
   ]},
   {"tag":[ 
      { 
         "path":"tags/1",
         "accountId":"3"
      }
   ]}
}

应该可以。

【讨论】:

    【解决方案2】:

    您发现 JSON 是不正确的 - 这就像在 PHP 中拥有一个包含多个变量的对象,所有变量都称为 tag。您的 JSON 可能应该在一个 tag 元素下包含所有数据,类似于...

    { 
       "tag":[ 
          { 
             "path":"tags/1",
             "accountId":"1"
          },
          { 
             "path":"tags/2",
             "accountId":"1"
          },
          { 
             "path":"tags/1",
             "accountId":"2"
          },
          { 
             "path":"tags/2",
             "accountId":"2"
          },
          { 
             "path":"tags/1",
             "accountId":"3"
          }
       ]
    }
    

    更改它的代码会更新它(一旦现有数据正确)将类似于......

    // Fetch existing data
    $json = file_get_contents("a.json");
    // Decode data to array format
    $tags = json_decode($json, true );
    // Add new tag into the data
    $tags['tag'][] = ["path" => "tags/1", "accountId" => "4"];
    // Create new JSON encoded string
    $newJson = json_encode($tags, JSON_PRETTY_PRINT);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2020-01-09
      相关资源
      最近更新 更多