3 回答
TA贡献1797条经验 获得超6个赞
您的原始代码(第二部分,带有RECORD)只有一个问题:它假设RECORD是一个记录。但显然它是一个包含单个记录的数组。
至于 put truein json_decode,有这么多上下文其实并不重要,因为没有给出明确的好处或坏处。但是如果你确实true在那里使用,你需要相应地调整代码,因为true输出是嵌套数组,但没有它输出是嵌套对象和数组。
这是一个示例 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);
这是示例代码输出的内容:
Array
(
[attributes] => Array
(
[fullname] => *"* **** ****
[email] =>
[role] =>
[year] =>
[department] =>
[speciality] =>
)
)
Array
(
[attributes] => Array
(
[fullname] => *"* **** ****
[email] =>
[role] =>
[year] =>
[department] =>
[speciality] =>
)
)
如您所见,输出是相同的,即两种方式都可以正常工作。
- 3 回答
- 0 关注
- 154 浏览
添加回答
举报