将 JSON 和 SubTree 存储到 PHP 变量中
Store JSON with SubTree into PHP variables
我从 CRM 中获取了一个 JSON 对象用于数据库中的记录。从 CRM 服务返回的 Response 目前看起来像这样:
{
"data": [
{
"Owner": {
"name": "Full Name",
"id": "Some ID"
},
"Email": "example@example.com",
"$currency_symbol": "$",
"Other_Phone": null,
"Mailing_State": null,
"Other_State": null,
"Other_Country": null,
"Last_Activity_Time": "2019-12-05T17:14:50+05:30",
"Department": null,
"$state": "save",
"$process_flow": false,
"Assistant": null,
"Mailing_Country": null,
"id": "Some ID",
"$approved": true,
"Reporting_To": null,
"$approval": {
"delegate": false,
"approve": false,
"reject": false,
"resubmit": false
},
"Other_City": null,
"Created_Time": "2019-12-05T16:24:33+05:30",
"$editable": true,
"PersonID": #SomeID,
"Home_Phone": null,
"Created_By": {
"name": "Full Name",
"id": "Some ID"
},
"Secondary_Email": null,
"Description": null,
"Mailing_Zip": null,
"$review_process": null,
"Twitter": null,
"Other_Zip": null,
"Mailing_Street": null,
"Salutation": null,
"First_Name": null,
"Full_Name": "Full Name",
"Asst_Phone": null,
"Modified_By": {
"name": "Full Name",
"id": "Some ID"
},
"$review": null,
"Skype_ID": null,
"Phone": null,
"Email_Opt_Out": false,
"Modified_Time": "2019-12-05T17:14:50+05:30",
"Date_of_Birth": null,
"Mailing_City": null,
"Title": null,
"Other_Street": null,
"Mobile": null,
"Last_Name": "Full Name",
"Lead_Source": null,
"Tag": [],
"Fax": null
}
],
"info": {
"per_page": 200,
"count": 1,
"page": 1,
"more_records": false
}
}
我试图访问的字段是 'Full_Name' 字段。我尝试访问它的代码:
$result = curl_exec($ch);
$parsed = json_decode($result, true);
echo $parsed->Full_Name;
curl_close($ch);
我遇到的错误:
Notice: Trying to get property of non-object in
C:\xampp\htdocs\Project\crmFunctions.php on line 88
你可以生成为 $parsed['data'][0]['Full_Name']
你的例子 json 在 "DoctorID": #SomeID,
中也有错误,只需更正它,你就会得到你的输出
$array = '{
"data": [
{
"Owner": {
"name": "Shrihari Prakash",
"id": "Some ID"
},
"Email": "example@example.com",
"$currency_symbol": "$",
"Other_Phone": null,
"Mailing_State": null,
"Other_State": null,
"Other_Country": null,
"Last_Activity_Time": "2019-12-05T17:14:50+05:30",
"Department": null,
"$state": "save",
"$process_flow": false,
"Assistant": null,
"Mailing_Country": null,
"id": "Some ID",
"$approved": true,
"Reporting_To": null,
"$approval": {
"delegate": false,
"approve": false,
"reject": false,
"resubmit": false
},
"Other_City": null,
"Created_Time": "2019-12-05T16:24:33+05:30",
"$editable": true,
"DoctorID": "#SomeID",
"Home_Phone": null,
"Created_By": {
"name": "Shrihari Prakash",
"id": "Some ID"
},
"Secondary_Email": null,
"Description": null,
"Mailing_Zip": null,
"$review_process": null,
"Twitter": null,
"Other_Zip": null,
"Mailing_Street": null,
"Salutation": null,
"First_Name": null,
"Full_Name": "Shrihari Prakash",
"Asst_Phone": null,
"Modified_By": {
"name": "Shrihari Prakash",
"id": "Some ID"
},
"$review": null,
"Skype_ID": null,
"Phone": null,
"Email_Opt_Out": false,
"Modified_Time": "2019-12-05T17:14:50+05:30",
"Date_of_Birth": null,
"Mailing_City": null,
"Title": null,
"Other_Street": null,
"Mobile": null,
"Last_Name": "Shrihari Prakash",
"Lead_Source": null,
"Tag": [],
"Fax": null
}
],
"info": {
"per_page": 200,
"count": 1,
"page": 1,
"more_records": false
}
}';
$parsed = json_decode($array, true);
echo $parsed['data'][0]['Full_Name'];
我从 CRM 中获取了一个 JSON 对象用于数据库中的记录。从 CRM 服务返回的 Response 目前看起来像这样:
{
"data": [
{
"Owner": {
"name": "Full Name",
"id": "Some ID"
},
"Email": "example@example.com",
"$currency_symbol": "$",
"Other_Phone": null,
"Mailing_State": null,
"Other_State": null,
"Other_Country": null,
"Last_Activity_Time": "2019-12-05T17:14:50+05:30",
"Department": null,
"$state": "save",
"$process_flow": false,
"Assistant": null,
"Mailing_Country": null,
"id": "Some ID",
"$approved": true,
"Reporting_To": null,
"$approval": {
"delegate": false,
"approve": false,
"reject": false,
"resubmit": false
},
"Other_City": null,
"Created_Time": "2019-12-05T16:24:33+05:30",
"$editable": true,
"PersonID": #SomeID,
"Home_Phone": null,
"Created_By": {
"name": "Full Name",
"id": "Some ID"
},
"Secondary_Email": null,
"Description": null,
"Mailing_Zip": null,
"$review_process": null,
"Twitter": null,
"Other_Zip": null,
"Mailing_Street": null,
"Salutation": null,
"First_Name": null,
"Full_Name": "Full Name",
"Asst_Phone": null,
"Modified_By": {
"name": "Full Name",
"id": "Some ID"
},
"$review": null,
"Skype_ID": null,
"Phone": null,
"Email_Opt_Out": false,
"Modified_Time": "2019-12-05T17:14:50+05:30",
"Date_of_Birth": null,
"Mailing_City": null,
"Title": null,
"Other_Street": null,
"Mobile": null,
"Last_Name": "Full Name",
"Lead_Source": null,
"Tag": [],
"Fax": null
}
],
"info": {
"per_page": 200,
"count": 1,
"page": 1,
"more_records": false
}
}
我试图访问的字段是 'Full_Name' 字段。我尝试访问它的代码:
$result = curl_exec($ch);
$parsed = json_decode($result, true);
echo $parsed->Full_Name;
curl_close($ch);
我遇到的错误:
Notice: Trying to get property of non-object in C:\xampp\htdocs\Project\crmFunctions.php on line 88
你可以生成为 $parsed['data'][0]['Full_Name']
你的例子 json 在 "DoctorID": #SomeID,
中也有错误,只需更正它,你就会得到你的输出
$array = '{
"data": [
{
"Owner": {
"name": "Shrihari Prakash",
"id": "Some ID"
},
"Email": "example@example.com",
"$currency_symbol": "$",
"Other_Phone": null,
"Mailing_State": null,
"Other_State": null,
"Other_Country": null,
"Last_Activity_Time": "2019-12-05T17:14:50+05:30",
"Department": null,
"$state": "save",
"$process_flow": false,
"Assistant": null,
"Mailing_Country": null,
"id": "Some ID",
"$approved": true,
"Reporting_To": null,
"$approval": {
"delegate": false,
"approve": false,
"reject": false,
"resubmit": false
},
"Other_City": null,
"Created_Time": "2019-12-05T16:24:33+05:30",
"$editable": true,
"DoctorID": "#SomeID",
"Home_Phone": null,
"Created_By": {
"name": "Shrihari Prakash",
"id": "Some ID"
},
"Secondary_Email": null,
"Description": null,
"Mailing_Zip": null,
"$review_process": null,
"Twitter": null,
"Other_Zip": null,
"Mailing_Street": null,
"Salutation": null,
"First_Name": null,
"Full_Name": "Shrihari Prakash",
"Asst_Phone": null,
"Modified_By": {
"name": "Shrihari Prakash",
"id": "Some ID"
},
"$review": null,
"Skype_ID": null,
"Phone": null,
"Email_Opt_Out": false,
"Modified_Time": "2019-12-05T17:14:50+05:30",
"Date_of_Birth": null,
"Mailing_City": null,
"Title": null,
"Other_Street": null,
"Mobile": null,
"Last_Name": "Shrihari Prakash",
"Lead_Source": null,
"Tag": [],
"Fax": null
}
],
"info": {
"per_page": 200,
"count": 1,
"page": 1,
"more_records": false
}
}';
$parsed = json_decode($array, true);
echo $parsed['data'][0]['Full_Name'];