解码嵌套JSON PHP
我想获得网站的域名、dns、ip地址等数据,我发现以下JSON结构可以帮助我做到这一点。 但我在解析特定信息时出错,例如dns_记录->类型、dns_记录->目标等。解码嵌套JSON PHP,php,json,Php,Json,我想获得网站的域名、dns、ip地址等数据,我发现以下JSON结构可以帮助我做到这一点。 但我在解析特定信息时出错,例如dns_记录->类型、dns_记录->目标等。 以下是我的嵌套JSON代码: { "meta": { "code": 200, "time": 1477428559, "requestId": "1f1d72299d7138924196681ca628a699" }, "data": { "tutorialspoint.com": {
以下是我的嵌套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\u记录->类型
和dns\u记录->目标
$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;
}
}
}
// targets
foreach($targetArray as $target){
echo $target;
}
// types
foreach($typeArray as $type){
echo $type;
}
我为目标和类型创建了两个数组,您可以通过这种方式访问每个target
和type
$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;
}
}
}
// targets
foreach($targetArray as $target){
echo $target;
}
// types
foreach($typeArray as $type){
echo $type;
}
我想你需要这样的东西:
$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);}
}
}
}
$final\u array=json\u decode($array)
foreach($final_arrayas$key=>$value){
如果($key=='data'){
foreach($key2=>$val的值){
foreach($valas$key3=>$child\u array\u val){
如果($key3=='dns_记录')回显“”;
打印($child\u array\u val);}
}
}
}
首先通过$final\u array=json\u decode($array)将其转换为php数组,然后通过foreach循环获取元素,希望它能正常工作。为什么会有人在JSON中编码为JSON?您需要做的是json\u解码($var->dns\u记录)
@HassanTahir,我已经完成了json解码的一部分,但是我无法编写foreach
循环的确切语法