处理使用curl检索的JSON

时间:2012-07-24 22:28:36

标签: php json curl

我正在使用curl函数从gotowebinar url获取数据。这是代码

 $data=curl_exec($curl);


   @curl_close($curl);

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

我收到了这个输出:

[
    {
        "registrantKey": 12345,
        "firstName": "xxx",
        "lastName": "xxx",
        "email": "xxx@yahoo.com",
        "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": "xxx@yahoo.com",
        "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": "xxx@yahoo.com",
        "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": "xxx@visioninvesting.com",
        "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": "xxx@yahoo.com",
        "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": "xxx@yahoo.com",
        "status": "WAITING",
        "registrationDate": "2012-06-29T21:07:10Z",
        "joinUrl": "https://www1.gotomeeting.com/join/123/456",
        "timeZone": "America/Denver"
    }
]

我使用json_decode格式化数据,但它不起作用。我想格式化输出,以便我可以在程序中使用它的值。

2 个答案:

答案 0 :(得分:1)

这是一些简单的PHP代码,用于循环调用json_decode生成的对象。

$newdata = json_decode($data);

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

您可以从解码对象访问返回的json中看到的任何属性。

由于您获得了一个对象数组,您可以循环遍历每个条目(如上所示),或访问如下所示的特定条目:

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

希望有助于您入门。

答案 1 :(得分:0)

首先尝试将其转换为数组:

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