我正在创建一个API,但是最终服务器以JSON格式向我发送回信息,其中包括RECORD属性:
{
"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, true);
我尝试过:
$request_json["attributes"] = array(
"userid" => str_replace(" ","",$user_uid),
"fullname" => $jsonRestData->FULLNAME,
"email" => $jsonRestData->STUDENT_EMAIL,
"role" => $jsonRestData->STUDENT_STATUS,
"year" => $jsonRestData->STUDENT_ACADEMIC_YEAR,
"department" => $jsonRestData->STUDENT_DEPARTMENT,
"speciality" => $jsonRestData->STUDENT_SPECIALITY
);
我也尝试过:
$request_json["attributes"] = array(
"userid" => str_replace(" ","",$user_uid),
"fullname" => $jsonRestData->RECORD->FULLNAME,
"email" => $jsonRestData->RECORD->STUDENT_EMAIL,
"role" => $jsonRestData->RECORD->STUDENT_STATUS,
"year" => $jsonRestData->RECORD->STUDENT_ACADEMIC_YEAR,
"department" => $jsonRestData->RECORD->STUDENT_DEPARTMENT,
"speciality" => $jsonRestData->RECORD->STUDENT_SPECIALITY
);
对于第一个示例,我遇到一个错误: 未定义的属性:stdClass :: $ FULLNAME
第二次出现错误: 试图获取非对象的属性
答案 0 :(得分:1)
更新:
如果对“”进行了大写,则代码有效,请检查以下内容:http://sandbox.onlinephpfunctions.com/code/9b1192db07cb3876a9c80e9ca25f88406117c0b5
您的代码中的问题出在JSON字符串中,您必须全名将字符串固定在其中。 (修复结束字符串“”)
$json_string='{
"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": "****"
}]
}';
$json_array=json_decode($json_string,true);
$records=$json_array['RECORD'][0];
//example of use
echo "FULL NAME : ".$records['FULLNAME'];
您可以使用此链接来检查或验证JSON STRING:https://jsonformatter.curiousconcept.com/
答案 1 :(得分:1)
您的原始代码(第二部分,带有RECORD
的代码)只有一个问题:假设RECORD
是一条记录。但是显然,这是一个包含单个记录的数组。
关于将true
放在json_decode
中,在如此多的上下文中这并不重要,因为它没有给出明确的利弊。但是,如果您确实在此处使用true
,则需要相应地调整代码,因为使用true
时,输出是嵌套数组,而没有使用true
时,输出是嵌套对象和数组。
这里有一个示例PHP,展示了两种实现方式-使用<?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] =>
)
)
这是示例代码输出的内容:
public class client {
private static final AtomicLong counter = new AtomicLong();
private final long id;
public client() {
this.id = counter.getAndIncrement();
}
public long getId() {
return id;
}
}
如您所见,输出是相同的,即两种方法都可以正常工作。
答案 2 :(得分:0)
您必须使用$data = json_decode($jsondata, true);
之后,您可以将数据作为数组访问