我使用cURL通过API访问数据。返回的结果是JSON。我试图访问返回数据的一部分,以向用户显示相关信息。以下是返回内容的示例:
[{
"FirstName": "Person1",
"LastName": "Guy",
"SISId": "7",
"MiddleName": "",
"EmailAddress": "demo@yahoo.com",
"FederalId": "",
"StateId": "",
"ActiviationDate": "\/Date(1375751488440)\/",
"ExpirationDate": null,
"Status": 1,
"UserId": 5632414,
"MembershipId": 5632414,
"Role": 5,
"IsOnline": false,
"HasUnreadMessages": false,
"LastLoginDate": null,
"LastActivityDate": null
}, {
"FirstName": "Person2",
"LastName": "Girl",
"SISId": "18",
"MiddleName": "Bailey",
"EmailAddress": "superstar08_03@yahoo.com",
"FederalId": "",
"StateId": "",
"ActiviationDate": "\/Date(1376928013777)\/",
"ExpirationDate": null,
"Status": 1,
"UserId": 5932535,
"MembershipId": 5932535,
"Role": 5,
"IsOnline": false,
"HasUnreadMessages": false,
"LastLoginDate": null,
"LastActivityDate": null
}]
我尝试了几种不同的方法,使用PHP尝试访问UserId
数据,但没有成功。这是我最新的代码:
<?php
//API CALL IS MADE ABOVE THIS LINE
$ch = curl_init();
curl_setopt_array($ch, $options);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if ($output === false || $info['http_code'] != 200) {
$output = "No cURL data returned [" . $info['http_code'] . "]";
if (curl_error($ch)) {
$output .= "\n" . curl_error($ch);
}
}
$object = json_decode($output);
print $obj->{'FirstName'}; // Person1
?>
答案 0 :(得分:1)
$object
是一个数组。
如果你想要一个人:
echo $object[0]->FirstName;