解码嵌套的JSON PHP

时间:2016-10-26 02:46:08

标签: php json

我想获取网站的域名,密码,IP地址等数据,并且我发现了以下可以帮助我这样做的JSON结构。 但是我在解析特定信息时遇到错误,例如dns_records-> type,dns_records-> target等。

这是我的嵌套JSON代码:

{
  "meta": {
    "code": 200,
    "time": 1477428559,
    "requestId": "1f1d72299d7138924196681ca628a699"
  },
  "data": {
    "tutorialspoint.com": {
      "updated_datetime": "2015-08-17 13:56:03",
      "domain_status": "[\"clientDeleteProhibited http:\\/\\/www.icann.org\\/epp#clientDeleteProhibited\",\"clientRenewProhibited http:\\/\\/www.icann.org\\/epp#clientRenewProhibited\",\"clientTransferProhibited http:\\/\\/www.icann.org\\/epp#clientTransferProhibited\",\"clientUpdateProhibited http:\\/\\/www.icann.org\\/epp#clientUpdateProhibited\"]",
      "full_whois": "Domain Name: TUTORIALSPOINT.COM",
      "dns_records": "[{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"A\",\"ip\":\"66.135.33.172\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns44.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns43.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"SOA\",\"mname\":\"ns43.domaincontrol.com\",\"rname\":\"dns.jomax.net\",\"serial\":2015080700,\"refresh\":28800,\"retry\":7200,\"expire\":604800,\"minimum-ttl\":3600},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":1,\"target\":\"ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT1.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT2.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT3.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT4.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"v=spf1 mx include:_spf.google.com -all\",\"entries\":[\"v=spf1 mx include:_spf.google.com -all\"]},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\",\"entries\":[\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\"]}]",
      "owner_email": null,
      "registrar": "GODADDY.COM, LLC",
      "registrar_slug": "godaddy.com-llc",
      "hosted_ip": "66.135.33.172",
      "hosted_ip_latitude": "29.4997",
      "hosted_ip_longitude": "-98.3992",
      "hosted_ip_country": "US",
      "hosted_ip_friendly_location": "Texas, San Antonio, United States, 78218",
      "hosted_ip_full_country": "United States"
    }
  }
}

如何从上面的代码中找到dns_records->typedns_records->target

2 个答案:

答案 0 :(得分:1)

dns_records包含更多typetarget,因此您可以通过这种方式访问​​所有类型和目标,

$record = '{
  "meta": {
    "code": 200,
    "time": 1477428559,
    "requestId": "1f1d72299d7138924196681ca628a699"
  },
  "data": {
    "tutorialspoint.com": {
      "updated_datetime": "2015-08-17 13:56:03",
      "domain_status": "[\"clientDeleteProhibited http:\\/\\/www.icann.org\\/epp#clientDeleteProhibited\",\"clientRenewProhibited http:\\/\\/www.icann.org\\/epp#clientRenewProhibited\",\"clientTransferProhibited http:\\/\\/www.icann.org\\/epp#clientTransferProhibited\",\"clientUpdateProhibited http:\\/\\/www.icann.org\\/epp#clientUpdateProhibited\"]",
      "full_whois": "Domain Name: TUTORIALSPOINT.COM",
      "dns_records": "[{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"A\",\"ip\":\"66.135.33.172\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns44.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"NS\",\"target\":\"ns43.domaincontrol.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"SOA\",\"mname\":\"ns43.domaincontrol.com\",\"rname\":\"dns.jomax.net\",\"serial\":2015080700,\"refresh\":28800,\"retry\":7200,\"expire\":604800,\"minimum-ttl\":3600},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":1,\"target\":\"ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT1.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":5,\"target\":\"ALT2.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT3.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"MX\",\"pri\":10,\"target\":\"ALT4.ASPMX.L.GOOGLE.com\"},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"v=spf1 mx include:_spf.google.com -all\",\"entries\":[\"v=spf1 mx include:_spf.google.com -all\"]},{\"host\":\"tutorialspoint.com\",\"class\":\"IN\",\"ttl\":3599,\"type\":\"TXT\",\"txt\":\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\",\"entries\":[\"google-site-verification=-RNr-P1jBNMarh7tMQEgXtlBVUi000DUph-h8H7uSaQ\"]}]",
      "owner_email": null,
      "registrar": "GODADDY.COM, LLC",
      "registrar_slug": "godaddy.com-llc",
      "hosted_ip": "66.135.33.172",
      "hosted_ip_latitude": "29.4997",
      "hosted_ip_longitude": "-98.3992",
      "hosted_ip_country": "US",
      "hosted_ip_friendly_location": "Texas, San Antonio, United States, 78218",
      "hosted_ip_full_country": "United States"
    }
  }
}';


$record = json_decode($record, true);

$dnsRecords = json_decode($record['data']['tutorialspoint.com']['dns_records'], true);

$typeArray = array();
$targetArray = array();
 foreach($dnsRecords as $row){
    foreach($row as $key=>$res){
        if($key == 'type'){
            $typeArray[] = $res;
        }
        if($key == 'target'){
            $targetArray[] = $res;
        }
    }
}

我为目标和类型创建了两个数组,您可以通过这种方式访问​​每个targettype

// targets
foreach($targetArray as $target){
    echo $target;
}

// types
foreach($typeArray as $type){
    echo $type;
}

答案 1 :(得分:0)

我认为你需要这样的东西:

 $final_array= json_decode($array) 
    foreach ($final_arrayas $key => $value) {
            if ($key == 'data') {
                foreach ($value as $key2 => $val) {
                    foreach($val as $key3=> $child_array_val){
                   if($key3=='dns_records') echo "<pre>";
                    print_r($child_array_val);}
                }
            }
        }