3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個贊
您的原始代碼(第二部分,帶有RECORD)只有一個問題:它假設(shè)RECORD是一個記錄。但顯然它是一個包含單個記錄的數(shù)組。
至于 put truein json_decode,有這么多上下文其實(shí)并不重要,因?yàn)闆]有給出明確的好處或壞處。但是如果你確實(shí)true在那里使用,你需要相應(yīng)地調(diào)整代碼,因?yàn)閠rue輸出是嵌套數(shù)組,但沒有它輸出是嵌套對象和數(shù)組。
這是一個示例 PHP,它展示了兩種方法——使用true和不使用它。
<?php
$jsonRestData2 = '{
"RECORD": [{
"@ID": "1",
"FULLNAME": "*\"* **** ****",
"PHONE": "*******",
"CELLULAR": "********",
"LOGIN_STATUS": "*",
"LOGIN_STATUS_TEXT": "****",
"STUDENT_ACADEMIC_YEAR": "",
"STUDENT_DEPARTMENT": "",
"STUDENT_SPECIALITY": "",
"STUDENT_PHONE": "",
"STUDENT_CELLULARPHONE": "",
"STUDENT_ADDRESS": " *",
"STUDENT_EMAIL": "",
"STUDENT_STATUS": "",
"STUDENT_ID": "*",
"TEACHER_ID": "*******",
"CURRENTYEAR": "****",
"TOKEN": "*************",
"CURRENTFULLYEAR": "****"
}]
}';
$jsonRestData = json_decode($jsonRestData2);
$request_json = [];
$request_json["attributes"] = array(
"fullname" => $jsonRestData->RECORD[0]->FULLNAME,
"email" => $jsonRestData->RECORD[0]->STUDENT_EMAIL,
"role" => $jsonRestData->RECORD[0]->STUDENT_STATUS,
"year" => $jsonRestData->RECORD[0]->STUDENT_ACADEMIC_YEAR,
"department" => $jsonRestData->RECORD[0]->STUDENT_DEPARTMENT,
"speciality" => $jsonRestData->RECORD[0]->STUDENT_SPECIALITY,
);
print_r($request_json);
$jsonRestData = json_decode($jsonRestData2, true);
$request_json = [];
$request_json["attributes"] = array(
"fullname" => $jsonRestData['RECORD'][0]['FULLNAME'],
"email" => $jsonRestData['RECORD'][0]['STUDENT_EMAIL'],
"role" => $jsonRestData['RECORD'][0]['STUDENT_STATUS'],
"year" => $jsonRestData['RECORD'][0]['STUDENT_ACADEMIC_YEAR'],
"department" => $jsonRestData['RECORD'][0]['STUDENT_DEPARTMENT'],
"speciality" => $jsonRestData['RECORD'][0]['STUDENT_SPECIALITY'],
);
print_r($request_json);
這是示例代碼輸出的內(nèi)容:
Array
(
[attributes] => Array
(
[fullname] => *"* **** ****
[email] =>
[role] =>
[year] =>
[department] =>
[speciality] =>
)
)
Array
(
[attributes] => Array
(
[fullname] => *"* **** ****
[email] =>
[role] =>
[year] =>
[department] =>
[speciality] =>
)
)
如您所見,輸出是相同的,即兩種方式都可以正常工作。

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個贊
你必須使用 $data = json_decode($jsondata, true);
之后,您可以將數(shù)據(jù)作為數(shù)組訪問
- 3 回答
- 0 關(guān)注
- 184 瀏覽
添加回答
舉報