简体   繁体   中英

How to extract data from a decoded JSON object in php

I wanted to try to get data from a JSON string which is loaded from another page. I currently have used Curl to get the data from the webpage but I can't acces the data in it.

I've already tried:

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

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

But this does't seem to work as it always returns 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));

First decode the JSON, then get the properties you want. Like this:

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

this is working for me.

<?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);
}
?>

if you want to get date of all index then try this in loop.

Firstly if your using GET there is no need to use CURL,

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

Will work just as well without any of the overhead. I suspect that your CURL isn't returning the page content so using file_get_contents() will fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM