为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
胡说叔叔 2021-08-28 18:32:53
我正在创建一个 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第二个我收到一个错误:试图获取非对象的属性
查看完整描述

3 回答

?
FFIVE

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] =>

        )


)

如您所见,输出是相同的,即两种方式都可以正常工作。


查看完整回答
反对 回复 2021-08-28
?
慕容3067478

TA贡献1773条经验 获得超3个赞

你必须使用 $data = json_decode($jsondata, true);

之后,您可以将数据作为数组访问


查看完整回答
反对 回复 2021-08-28
  • 3 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信