【发布时间】:2017-12-22 10:51:34
【问题描述】:
我在以正确方式在 JSON 对象中插入一些数据时遇到了一些问题。我希望 JSON 对象在对象顶部没有任何键。像这样说明的是我现在的 JSON:
现在的 JSON:
{
"Account2": [{
"accountId": "",
"containerId": "",
"tag": 1,
"trigger": 1,
"variable": 4,
"account_name": "Account2"
}, {
"accountId": "",
"containerId": "",
"missing_live": true
}],
"Account 1": [{
"accountId": "",
"containerId": "",
"tag": 2,
"trigger": 1,
"variable": 1,
"account_name": "Account 1"
}],
"GTMdocx": [{
"accountId": "",
"containerId": "",
"tag": 0,
"trigger": 0,
"variable": 1,
"account_name": "GTMdocx"
}]
}
如您所见,“Account2”、“Account 1”、“GTMdocx”等。它们就像 json 对象中的 account_name。我想删除“top”键并保留“account_name”。但是当我删除代码中的行时,我只得到一个响应(一个 JSON 对象)而不是全部 3。
PHP 代码:
try { // Some containers might not have live-version
$container_version = self::listAccountsContainersVersion($account_container['path']);
$account_container->tag = count($container_version['tag']);
$account_container->trigger = count($container_version['trigger']);
$account_container->variable = count($container_version['variable']);
$account_container->account_name = $account['name'];
// Add the whole account to the main container
$containers[$account['name']][] = $account_container;
} catch (\Exception $e) {
$account_container->missing_live = true;
$containers[$account['name']][] = $account_container;
//$containers[$account['name']]['missing_live_version'] = $account_container; //Container not published
}
ive commented // 将整个帐户添加到主容器中...这是所有错误的行。我尝试将其更改为:
$containers[$account] = $account_container;
所以我将得到所有$account JSON 对象包含在$account_container 中,但如果我没有$account['name'],则响应在其3 时仅显示一个对象。(这是在foreach 循环内)。
所以我希望一个 JSON 对象看起来像这样:
{
"1": {
"accountId": "",
"containerId": "",
"tag": 1,
"trigger": 1,
"variable": 4,
"account_name": "Account2"
},
"2": {
"accountId": "",
"containerId": "",
"missing_live": true,
"account_name": "Account2"
},
"3": {
"accountId": "",
"containerId": "",
"tag": 2,
"trigger": 1,
"variable": 1,
"account_name": "Account 1"
},
"4": {
"accountId": "",
"containerId": "",
"tag": 0,
"trigger": 0,
"variable": 1,
"account_name": "GTMdocx"
},
"5": {
"accountId": "",
"containerId": "",
"tag": 0,
"trigger": 0,
"variable": 0,
"account_name": "Account3"
}
}
您可以看到我删除了顶部的“Account2”。
【问题讨论】:
-
非法偏移类型在 (...)