假设下面是我的JSON数据
{"pricing": {
"com": {
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"org": {
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"net": {
"addons": {
"dns": true,
"email": true,
"idprotect": true
}
}}
我想从JSON上面只显示(com,org,net)。我们怎么能这样做?
答案 0 :(得分:1)
执行此操作的选项是使用json_decode并将true
传递给第二个参数,以将返回的对象转换为关联数组。
要仅显示您可以使用foreach循环$output["pricing"]
的密钥并显示密钥:
$json = '{"pricing": {"com": {"addons": {"dns": true,"email": true,"idprotect": true}},"org": {"addons": {"dns": true,"email": true,"idprotect": true}},"net": {"addons": {"dns": true,"email": true,"idprotect": true}}}}';
$output = json_decode($json, true);
foreach ($output["pricing"] as $key => $value) {
echo $key . "<br>";
}
另一种方法是获取array_keys并循环它们:
foreach (array_keys($output["pricing"]) as $key) {
echo $key . "<br>";
}
答案 1 :(得分:0)
首先你有一个不正确的JSON格式检查出来。我认为这个解决方案可能对你有帮助!!
$json = '{"pricing": {"com": {"addons": {"dns": true,"email": true,"idprotect": true}},"org": {"addons": {"dns": true,"email": true,"idprotect": true}},"net": {"addons": {"dns": true,"email": true,"idprotect": true}}}}';
$output = json_decode($json);
print_r($output->pricing->com->addons);
print_r($output->pricing->org->addons);
print_r($output->pricing->net->addons);
你会得到这样的东西!!
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 )
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 )
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 )
答案 2 :(得分:0)
你在找这个吗?
$json = '{"pricing": {"com": {"addons": {"dns": true,"email": true,"idprotect": true}},"org": {"addons": {"dns": true,"email": true,"idprotect": true}},"net": {"addons": {"dns": true,"email": true,"idprotect": true}}}}';
$output = json_decode($json,true);
echo implode(",",array_keys($output["pricing"]));
玉米,组织,净
答案 3 :(得分:-1)
您可以轻松完成:
$parsed_json= json_decode("your raw data here");
$com = $parsed_json["com"];
$org = $parsed_json["org"];
$net = $parsed_json["net"];
您可以在PHP文档网站上了解有关json_decode的更多信息。