4

我从 api 请求返回到 freebase 数据库的 json 结果。这是返回的名为 $json 的对象的一部分。$json 的 var 转储:

stdClass Object
(
[name] => Abomey
[/location/statistical_region/population_growth_rate] => 
[/common/topic/article] => Array
    (
        [0] => stdClass Object
            (
                [id] => /m/0jk2c
            )
    )

如何减去 /m/0jk2c 部分?

$json->/common/topic/article[0]->id (显然)不起作用。

4

2 回答 2

10

这应该这样做:

$json->{"/common/topic/article"}[0]->id
于 2012-04-24T21:22:32.870 回答
0

这是你应该使用的

 $class->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id

你的对象看起来像这样的原因

$std = new stdClass();
$std->id = '/m/0jk2c' ;

$json  = new stdClass();
$json->name = "Abomey" ;
$json->{'/location/statistical_region/population_growth_rate'} = array('/common/topic/article'=>array($std));

如果你跑

var_dump($json->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id);

输出

string '/m/0jk2c' (length=8)

echo "<pre>";
print_r($json);

输出

stdClass Object
(
    [name] => Abomey
    [/location/statistical_region/population_growth_rate] => Array
        (
            [/common/topic/article] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => /m/0jk2c
                        )

                )

        )

)
于 2012-04-24T21:27:36.280 回答