douhuan1908 2012-11-25 05:36
浏览 39
已采纳

json解码成阵列

I'm trying to store the titles in an array and extract the long cmcontinue string from the below url.

http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:1980_births&format=json

my current code:

$url = 'http://en.wikipedia.org/w/api.php?
action=query&list=categorymembers&cmtitle=Category:'.$cat.'&format=json';

$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "asdf");

$c = curl_exec($ch);
$json = json_decode($c);

$array = $json->{'query'}->{'categorymembers'}->{'title'};
  • 写回答

1条回答 默认 最新

  • douchun1900 2012-11-25 06:00
    关注

    try adding second paremeter of json_decode, like:

    $json = json_decode($c, true);
    

    And get cmcontinue value as:

    echo $json["query-continue"]["categorymembers"]["cmcontinue"];
    

    For titles:

    $titles = array();
    foreach($json["query"]["categorymembers"] as $vals) {
        array_push($titles, $vals["title"]);
    }
    
    echo "<pre>"; print_r($titles);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 数字信号处理实验报告
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程