【问题标题】:Fetch JSON data, wikimedia获取 JSON 数据,维基媒体
【发布时间】:2016-04-10 20:18:36
【问题描述】:

问题

我有从 wikimedia API 获得的 JSON 格式的 JSON 内容。我想提取 [*] 这个下的数据。虽然在调用页面 id 之前我不知道,所以我不能在中间使用 pageid。我也不知道如何通过 astrics ( * )。我使用以下代码来获取数据,但出现错误。

非常感谢任何帮助或指导。

代码

$api_data->query->pages->revisions[0]->['*'];

JSON

stdClass Object
(
    [batchcomplete] => 
    [query] => stdClass Object
        (
            [pages] => stdClass Object
                (
                    [27000] => stdClass Object
                        (
                            [pageid] => 27000
                            [ns] => 0
                            [title] => Patna
                            [revisions] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [contentformat] => text/x-wiki
                                            [contentmodel] => wikitext
                                            [*] => ==Understand==
The ancient name of Patna was 'Pataliputra' and it was the capital of the Maurya and Gupta empires. Located at the site where Patna is today, the ancient city of Patliputra, with a glorious period of history spanning a thousand years (500BC-400AD), saw the rise and fall of India's first major kingdoms.

Lying along the banks of the Ganges River, Patna is surrounded by important religious centers for the Buddhists, Sikhs and Jains. This city has been home to two great religions, Buddhism and Jainism, and myriad dynasties from ancient to modern times.
                                        )

                                )

                        )

                )

        )

)

【问题讨论】:

  • $api_data->query->pages->revisions[0]->{'*'};
  • 感谢您的帮助!!

标签: php json mediawiki wikipedia-api


【解决方案1】:

您可以按照 Chay22 的建议在括号中使用属性名称:

$data->first = 'hello';
$data->{'*'} = 'world';

var_dump($data);

// Like this
echo $data->{'*'};
echo PHP_EOL;

// or like this
$varname = '*';

echo $data->{$varname};
echo PHP_EOL;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多