【问题标题】:How to extract data from a decoded JSON object in php如何从php中解码的JSON对象中提取数据
【发布时间】:2016-01-19 13:01:23
【问题描述】:

我想尝试从另一个页面加载的 JSON 字符串中获取数据。我目前使用 Curl 从网页中获取数据,但无法访问其中的数据。

我已经试过了:

var_dump(json_decode($result->version, true));

var_dump(json_decode($result[3][0]["date"], true));

但这似乎不起作用,因为它总是返回 NULL

$url="https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));

【问题讨论】:

  • $result 包含 JSON?如果尝试print_r($result);这里的输出是什么?
  • @AddWebSolutionPvtLtd 它包含一个字符串prntscr.com/9s2du5
  • @ThomH 你试过不给 json_decode() 的第二个参数,可能是它无法生成关联数组。

标签: php json curl


【解决方案1】:

先解码 JSON,然后得到你想要的属性。像这样:

$yourObject = json_decode($result);
var_dump($youObject->version);

【讨论】:

    【解决方案2】:

    这对我有用。

    <?php
        $url = "https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201";
    //  Initiate curl
        $ch = curl_init();
    // Disable SSL verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // Will return the response, if false it print the response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Set the url
        curl_setopt($ch, CURLOPT_URL, $url);
    // Execute
        $result = curl_exec($ch);
    // Closing
        curl_close($ch);
    
    // Will dump a beauty json :3
        $data = json_decode($result);
    //echo $data->data[0]['date'];
        echo "<pre>";
        print_r($data->data[0]->date);
    }
    ?>
    

    如果您想获取所有 index 的日期,请在循环中尝试。

    【讨论】:

      【解决方案3】:

      首先如果你使用 GET 就不需要使用 CURL,

      $result = file_get_contents(https://roosters.deltion.nl/api/roster?group=AO2B&start=20160125&end=20160201);
      

      在没有任何开销的情况下也能正常工作。我怀疑您的 CURL 没有返回页面内容,因此使用 file_get_contents() 将修复它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 2019-08-10
        • 1970-01-01
        相关资源
        最近更新 更多