user1515728
user1515728

Reputation: 61

Processing JSON retrieved with curl

I am using curl function to get the data from gotowebinar url. here is the code

 $data=curl_exec($curl);


   @curl_close($curl);

   $newdata=json_decode($data, true);
   print_r($newdata);

I am getting this output:

[
    {
        "registrantKey": 12345,
        "firstName": "xxx",
        "lastName": "xxx",
        "email": "[email protected]",
        "status": "WAITING",
        "registrationDate": "2012-07-11T16:54:11Z",
        "joinUrl": "https://www1.gotomeeting.com/join/123/456",
        "timeZone": "America/New_York"
    },
    {
        "registrantKey": 12345,
        "firstName": "xxx",
        "lastName": "xxx",
        "email": "[email protected]",
        "status": "WAITING",
        "registrationDate": "2012-07-05T23:55:23Z",
        "joinUrl": "https://www1.gotomeeting.com/join/123/456",
        "timeZone": "America/New_York"
    },
    {
        "registrantKey": 12345,
        "firstName": "xxx",
        "lastName": "xxx",
        "email": "[email protected]",
        "status": "WAITING",
        "registrationDate": "2012-07-11T23:27:56Z",
        "joinUrl": "https://www1.gotomeeting.com/join/123/456",
        "timeZone": "America/Chicago"
    },
    {
        "registrantKey": 12345,
        "firstName": "xxx",
        "lastName": "xxx",
        "email": "[email protected]",
        "status": "WAITING",
        "registrationDate": "2012-07-11T23:29:40Z",
        "joinUrl": "https://www1.gotomeeting.com/join/123/456",
        "timeZone": "America/Chicago"
    },
    {
        "registrantKey": 12345,
        "firstName": "xxx",
        "lastName": "xxx",
        "email": "[email protected]",
        "status": "WAITING",
        "registrationDate": "2012-07-11T18:14:32Z",
        "joinUrl": "https://www1.gotomeeting.com/join/123/456",
        "timeZone": "America/Chicago"
    },
    {
        "registrantKey": 12345,
        "firstName": "test",
        "lastName": "1",
        "email": "[email protected]",
        "status": "WAITING",
        "registrationDate": "2012-06-29T21:07:10Z",
        "joinUrl": "https://www1.gotomeeting.com/join/123/456",
        "timeZone": "America/Denver"
    }
]

I used json_decode to format the data but it did not work. I want to format the output so that I can use its values in program.

Upvotes: 0

Views: 188

Answers (2)

David
David

Reputation: 3822

Try converting it to an array first:

$newdata = (array) json_decode($data, true);

Upvotes: 0

drew010
drew010

Reputation: 69927

Here is some simple PHP code for looping over the resulting object from calling json_decode.

$newdata = json_decode($data);

foreach($newdata as $entry) {
    echo "{$entry->firstName} is {$entry->status}.  " .
         "Their key is {$entry->registrantKey}.<br />\n";
}

You can access any of the properties you see in the returned json from the decoded object.

Since you get an array of objects, you can loop over each entry (as shown above), or access a specific entry like this:

$third = $newdata[2]->firstName;

Hope that helps get you started.

Upvotes: 1

Related Questions