[英]extract from json object thats converte to array php
I try to extract data from an json object. 我尝试从json对象提取数据。
I mannaged to convert the json object to an array but i can't extract the data i want The array and var_dump can be found here http://pastebin.com/f1ujbSCq 我强迫将json对象转换为数组,但无法提取所需的数据。可以在此处找到数组和var_dump http://pastebin.com/f1ujbSCq
if you look in the array at object->object->attributes-attribute 如果您在数组中查看object-> object-> attributes-attribute
You see values in the object class. 您会在对象类中看到值。 for example i want the the value where the name in the onject class is descr or admin-c,tech-c. 例如,我想要在onject类中的名称为descr或admin-c,tech-c的值。
But i can't figer out how to do that 但是我不知道该怎么做
THis is the php code i use to extract data from json object to an array: 这是我用来将数据从json对象提取到数组的php代码:
$domeinnaam = "2.0.0.0.0.0.0.1.8.6.9.0.2.0.a.2.ip6.arpa";
$ripeinfo = haalripezone($domeinnaam);
var_dump($ripeinfo);
print_r($ripeinfo);
function haalripezone ($domeinnaam)
{
$link = "https://rest.db.ripe.net/ripe/domain/$domeinnaam.json";
$downloadlink = file_get_contents($link);
$result = json_decode($downloadlink);
try 尝试
$result = json_decode($downloadlink, true);
This way the decode will be totally put into an array, then accessing the data will be easier 这样,解码将被完全放入一个数组中,然后访问数据将变得更加容易
You see values in the object class. 您会在对象类中看到值。 for example i want the the value where the name in the onject class is descr or admin-c,tech-c. 例如,我想要在onject类中的名称为descr或admin-c,tech-c的值。
Since you have already decoded the JSON as an object, You could access your fields like this.. 由于您已经将JSON解码为对象,因此可以像这样访问字段。
echo $yourObject->objects->attributes->attribute->name; //"prints" admin-c
You could alternatively loop through using a foreach
construct.. 您也可以使用foreach
构造来循环foreach
。
<?php
$domeinnaam = "2.0.0.0.0.0.0.1.8.6.9.0.2.0.a.2.ip6.arpa";
$ripeinfo = haalripezone($domeinnaam);
echo "<pre>";
foreach($ripeinfo->objects->object[0]->attributes->attribute as $attr)
{
echo $attr->name."<br>";
}
function haalripezone ($domeinnaam)
{
$link = "https://rest.db.ripe.net/ripe/domain/$domeinnaam.json";
//$downloadlink = file_get_contents($link);
$downloadlink = '{"objects":{"object":[{"type":"domain","link":{"type":"locator","href":"http://rest.db.ripe.net/ripe/domain/2.0.0.0.0.0.0.1.8.6.9.0.2.0.a.2.ip6.arpa"},"source":{"grs-mirror":[],"id":"ripe"},"primary-key":{"attribute":[{"name":"domain","value":"2.0.0.0.0.0.0.1.8.6.9.0.2.0.a.2.ip6.arpa"}]},"attributes":{"attribute":[{"name":"domain","value":"2.0.0.0.0.0.0.1.8.6.9.0.2.0.a.2.ip6.arpa"},{"name":"descr","value":"Reverse delegation for Glue network ipv6 tunnel server"},{"name":"nserver","value":"ns3.hobby.nl"},{"name":"nserver","value":"ns2.hobby.nl"},{"name":"nserver","value":"ns1.hobby.nl"},{"link":{"type":"locator","href":"http://rest.db.ripe.net/ripe/role/HNET2-RIPE"},"name":"admin-c","value":"HNET2-RIPE","referenced-type":"role"},{"link":{"type":"locator","href":"http://rest.db.ripe.net/ripe/role/HNET2-RIPE"},"name":"tech-c","value":"HNET2-RIPE","referenced-type":"role"},{"link":{"type":"locator","href":"http://rest.db.ripe.net/ripe/role/HNET2-RIPE"},"name":"zone-c","value":"HNET2-RIPE","referenced-type":"role"},{"link":{"type":"locator","href":"http://rest.db.ripe.net/ripe/mntner/HOBBYNET-RIPE-MNT"},"name":"mnt-by","value":"HOBBYNET-RIPE-MNT","referenced-type":"mntner"},{"name":"source","value":"RIPE","comment":"Filtered"}]},"tags":{"tag":[]}}]},"terms-and-conditions":{"type":"locator","href":"http://www.ripe.net/db/support/db-terms-conditions.pdf"}}';
$result = json_decode($downloadlink);
return $result;
}
OUTPUT :
domain
descr
nserver
nserver
nserver
admin-c
tech-c
zone-c
mnt-by
source
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.