我有这个PHP代码:
<?php
session_start();
$data = json_decode($_SESSION['data']);
print_r($data);
?>
从会话中提供我的数据。我打印它和
stdClass Object ( [_readyCallbacks] => Array ( [0] => ) [http] => stdClass Object ( [_cookie] => SESSION_ID=92f5d674-2430-4ee1-bfaf-04d7be2d32a7; M6UserName=8290 ) [magisterSchool] => stdClass Object ( [id] => 2a685bd6-4cab-4d51-bf48-07ac41ad6c7d [name] => CS Vincent van Gogh [url] => https://csvvg.magister.net ) [username] => 8290 [_keepLoggedIn] => 1 [_ready] => 1 [_magisterLoadError] => [_sessionId] => 92f5d674-2430-4ee1-bfaf-04d7be2d32a7 [_id] => 8982 [_personUrl] => https://csvvg.magister.net/api/personen/8982 [_pupilUrl] => https://csvvg.magister.net/api/leerlingen/8982 [_profileInfo] => stdClass Object ( [_firstName] => Robin [_lastName] => Noord [_birthDate] => 1999-01-23T00:00:00.000Z [_id] => 8982 [_officialFirstNames] => Robin [_initials] => R. [_namePrefix] => van der [_officialSurname] => Noord [_birthSurname] => [_birthNamePrefix] => [_useBirthname] => [_isChild] => ) [_messageFolders] => Array ( [0] => stdClass Object ( [_name] => Postvak IN [_unreadMessagesCount] => 0 [_id] => 1 [_parentId] => 0 ) [1] => stdClass Object ( [_name] => Verzonden items [_unreadMessagesCount] => 0 [_id] => 2 [_parentId] => 0 ) [2] => stdClass Object ( [_name] => Verwijderde items [_unreadMessagesCount] => 0 [_id] => 3 [_parentId] => 0 ) [3] => stdClass Object ( [_name] => Mededelingen [_unreadMessagesCount] => 0 [_id] => 4 [_parentId] => 0 ) ) )
所以我想知道,我如何得到,例如firstName
(profileInfo
)中的值,例如username
?
我最初得到的数据只是JSON:
{
"_readyCallbacks": [
null
],
"http": {
"_cookie": "SESSION_ID=92f5d674-2430-4ee1-bfaf-04d7be2d32a7; M6UserName=8290"
},
"magisterSchool": {
"id": "2a685bd6-4cab-4d51-bf48-07ac41ad6c7d",
"name": "CS Vincent van Gogh",
"url": "https:\/\/csvvg.magister.net"
},
"username": "8290",
"_keepLoggedIn": true,
"_ready": true,
"_magisterLoadError": null,
"_sessionId": "92f5d674-2430-4ee1-bfaf-04d7be2d32a7",
"_id": 8982,
"_personUrl": "https:\/\/csvvg.magister.net\/api\/personen\/8982",
"_pupilUrl": "https:\/\/csvvg.magister.net\/api\/leerlingen\/8982",
"_profileInfo": {
"_firstName": "Robin",
"_lastName": "Noord",
"_birthDate": "1999-01-23T00:00:00.000Z",
"_id": 8982,
"_officialFirstNames": "Robin",
"_initials": "R.",
"_namePrefix": "van der",
"_officialSurname": "Noord",
"_birthSurname": null,
"_birthNamePrefix": null,
"_useBirthname": false,
"_isChild": false
},
"_messageFolders": [
{
"_name": "Postvak IN",
"_unreadMessagesCount": 0,
"_id": 1,
"_parentId": 0
},
{
"_name": "Verzonden items",
"_unreadMessagesCount": 0,
"_id": 2,
"_parentId": 0
},
{
"_name": "Verwijderde items",
"_unreadMessagesCount": 0,
"_id": 3,
"_parentId": 0
},
{
"_name": "Mededelingen",
"_unreadMessagesCount": 0,
"_id": 4,
"_parentId": 0
}
]
}
主要问题在于它是一个嵌套的JSON,包含{}&#39; s和[]&#39; s和_&#39; s,这些都让它有点混乱。
答案 0 :(得分:0)
您可以使用
访问它$data['http'] ['username']
在json中,使用{}
将对象括起来,并在使用json_decode()
时将其翻译成密钥对数组
答案 1 :(得分:0)
尝试以这种方式访问stdClass对象属性:
// assumming that $json_str is your initial json string
$obj = json_decode($json_str);
var_dump($obj->_profileInfo->_firstName);
// the output:
string(5) "Robin"
答案 2 :(得分:0)
试试这个:$data->_profileInfo->_firstName;
答案 3 :(得分:0)
您可以解码为对象和访问对象属性:
$data = json_decode($_SESSION['data']);
echo $data->_profileInfo->_firstName
或解码为关联数组,然后:
$data = json_decode($_SESSION['data'], true);
echo $data['_profileInfo']['_firstName']