如何在php中提取内部JSON变量?

我正在创建一个 API,但最终服务器以包含 RECORD 属性的 JSON 格式向我发送信息:


{

    "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


第二个我收到一个错误:试图获取非对象的属性


胡说叔叔
浏览 155回答 3
3回答

FFIVE

您的原始代码(第二部分,带有RECORD)只有一个问题:它假设RECORD是一个记录。但显然它是一个包含单个记录的数组。至于 put truein json_decode,有这么多上下文其实并不重要,因为没有给出明确的好处或坏处。但是如果你确实true在那里使用,你需要相应地调整代码,因为true输出是嵌套数组,但没有它输出是嵌套对象和数组。这是一个示例 PHP,它展示了两种方法——使用true和不使用它。<?php$jsonRestData2 = '{&nbsp; &nbsp; "RECORD": [{&nbsp; &nbsp; &nbsp; &nbsp; "@ID": "1",&nbsp; &nbsp; &nbsp; &nbsp; "FULLNAME": "*\"* **** ****",&nbsp; &nbsp; &nbsp; &nbsp; "PHONE": "*******",&nbsp; &nbsp; &nbsp; &nbsp; "CELLULAR": "********",&nbsp; &nbsp; &nbsp; &nbsp; "LOGIN_STATUS": "*",&nbsp; &nbsp; &nbsp; &nbsp; "LOGIN_STATUS_TEXT": "****",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_ACADEMIC_YEAR": "",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_DEPARTMENT": "",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_SPECIALITY": "",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_PHONE": "",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_CELLULARPHONE": "",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_ADDRESS": " *",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_EMAIL": "",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_STATUS": "",&nbsp; &nbsp; &nbsp; &nbsp; "STUDENT_ID": "*",&nbsp; &nbsp; &nbsp; &nbsp; "TEACHER_ID": "*******",&nbsp; &nbsp; &nbsp; &nbsp; "CURRENTYEAR": "****",&nbsp; &nbsp; &nbsp; &nbsp; "TOKEN": "*************",&nbsp; &nbsp; &nbsp; &nbsp; "CURRENTFULLYEAR": "****"&nbsp; &nbsp; }]}';$jsonRestData = json_decode($jsonRestData2);$request_json = [];$request_json["attributes"] = array(&nbsp; &nbsp; "fullname"&nbsp; &nbsp;=> $jsonRestData->RECORD[0]->FULLNAME,&nbsp; &nbsp; "email"&nbsp; &nbsp; &nbsp; => $jsonRestData->RECORD[0]->STUDENT_EMAIL,&nbsp; &nbsp; "role"&nbsp; &nbsp; &nbsp; &nbsp;=> $jsonRestData->RECORD[0]->STUDENT_STATUS,&nbsp; &nbsp; "year"&nbsp; &nbsp; &nbsp; &nbsp;=> $jsonRestData->RECORD[0]->STUDENT_ACADEMIC_YEAR,&nbsp; &nbsp; "department" => $jsonRestData->RECORD[0]->STUDENT_DEPARTMENT,&nbsp; &nbsp; "speciality" => $jsonRestData->RECORD[0]->STUDENT_SPECIALITY,);print_r($request_json);$jsonRestData = json_decode($jsonRestData2, true);$request_json = [];$request_json["attributes"] = array(&nbsp; &nbsp; "fullname"&nbsp; &nbsp;=> $jsonRestData['RECORD'][0]['FULLNAME'],&nbsp; &nbsp; "email"&nbsp; &nbsp; &nbsp; => $jsonRestData['RECORD'][0]['STUDENT_EMAIL'],&nbsp; &nbsp; "role"&nbsp; &nbsp; &nbsp; &nbsp;=> $jsonRestData['RECORD'][0]['STUDENT_STATUS'],&nbsp; &nbsp; "year"&nbsp; &nbsp; &nbsp; &nbsp;=> $jsonRestData['RECORD'][0]['STUDENT_ACADEMIC_YEAR'],&nbsp; &nbsp; "department" => $jsonRestData['RECORD'][0]['STUDENT_DEPARTMENT'],&nbsp; &nbsp; "speciality" => $jsonRestData['RECORD'][0]['STUDENT_SPECIALITY'],);print_r($request_json);这是示例代码输出的内容:Array(&nbsp; &nbsp; [attributes] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [fullname] => *"* **** ****&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [role] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [year] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [department] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [speciality] =>&nbsp; &nbsp; &nbsp; &nbsp; ))Array(&nbsp; &nbsp; [attributes] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [fullname] => *"* **** ****&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [email] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [role] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [year] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [department] =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [speciality] =>&nbsp; &nbsp; &nbsp; &nbsp; ))如您所见,输出是相同的,即两种方式都可以正常工作。

慕容3067478

你必须使用&nbsp;$data = json_decode($jsondata, true);之后,您可以将数据作为数组访问
打开App,查看更多内容
随时随地看视频慕课网APP