在使用file_get_contents读取json文件后,我有一个issuse。
当我运行此代码时,它的工作正常:
<?php
$json='[
{
"fullName":"Shachar Ganot",
"address":"Yad Rambam",
"phoneNumber":"050-1231233",
"email":"",
"note":"",
"role":"",
"area":""
},
{
"fullName":"Betty Ganot",
"address":"Modiin",
"phoneNumber":"054-3213211",
"email":"",
"note":"",
"role":"",
"area":""
},
{
"fullName":"Someone Else",
"address":"Somewhere",
"phoneNumber":"123456789",
"email":"",
"note":"",
"role":"",
"area":""
}
]';
//$json = file_get_contents('Test.txt');
$data = json_decode($json,true);
echo $data[0]['fullName'];
?>
结果: Shachar Ganot
当我运行此代码时,它为空:
<?php
$json = file_get_contents('Test.txt');
$data = json_decode($json,true);
echo $data[0]['fullName'];
?>
结果: ****空 - Nothig出现****
运行此代码时,检查 file_get_contents 是否有效:
<?php
$json = file_get_contents('Test.txt');
$data = json_decode($json,true);
echo $json;
?>
结果:
[{&#34; fullName&#34;:&#34; Shachar Ganot&#34;,&#34;地址&#34;:&#34; Yad Rambam&#34;,&#34; phoneNumber&#34 ;:&#34; 050-1231233&#34;,&#34;电子邮件&#34;:&#34;&#34;,&#34;注意&#34;:&#34;&#34;,& #34;角色&#34;:&#34;&#34;,&#34;区域&#34;:&#34;&#34; },{&#34; fullName&#34;:&#34; Betty Ganot&#34;,&#34;地址&#34;:&#34; Modiin&#34;,&#34; phoneNumber&#34;:& #34; 054-3213211&#34;,&#34;电子邮件&#34;:&#34;&#34;,&#34;注意&#34;:&#34;&#34;,&#34;角色&#34;:&#34;&#34;,&#34;区域&#34;:&#34;&#34; },{&#34; fullName&#34;:&#34;其他人&#34;,&#34;地址&#34;:&#34;某处&#34;,&#34; phoneNumber&#34;:& #34; 123456789&#34;,&#34;电子邮件&#34;:&#34;&#34;,&#34;注意&#34;:&#34;&#34;,&#34;角色&# 34;:&#34;&#34;,&#34; area&#34;:&#34;&#34; }]
我错过了什么?
毋庸置疑,我做了JSON有效https://jsonformatter.curiousconcept.com/
答案 0 :(得分:8)