从json对象中提取转换为数组php

时间:2014-02-25 10:00:30

标签: php arrays json

我尝试从json对象中提取数据。

我管理将json对象转换为数组,但我无法提取我想要的数据 可以在http://pastebin.com/f1ujbSCq

找到数组和var_dump

如果您在object-> object-> attributes-attribute

中查看数组

您可以在对象类中看到值。 例如,我想要onject类中的名称是descr或admin-c,tech-c。

但我无法理解如何做到这一点

这是我用来从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);

2 个答案:

答案 0 :(得分:1)

尝试

  $result = json_decode($downloadlink, true);

这样解码将完全放入数组中,然后访问数据将更容易

答案 1 :(得分:1)

  

您可以在对象类中看到值。例如,我想要的价值   onject类中的名称是descr或admin-c,tech-c。

由于您已将JSON解码为对象,因此您可以像这样访问您的字段..

echo $yourObject->objects->attributes->attribute->name; //"prints" admin-c

您也可以循环使用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

Demo