无法从php中的JSON文件获取内容

无法从php中的JSON文件获取内容,php,mysql,json,Php,Mysql,Json,我试图从一个JSON文件中获取内容,并使用PHP将其放入MySQL数据库,以下是我迄今为止的尝试 这是JSON文件AK.JSON: [ { "CompanyName": "Logo Shirts Direct", "StreetAddress": "1001 Commerce Parkway South Dr Suite E", "Region": "Greenwood", "State": "IN", "PostCode": "46143", "

我试图从一个JSON文件中获取内容,并使用PHP将其放入MySQL数据库,以下是我迄今为止的尝试

这是JSON文件
AK.JSON

[
{
    "CompanyName": "Logo Shirts Direct",
    "StreetAddress": "1001 Commerce Parkway South Dr Suite E",
    "Region": "Greenwood",
    "State": "IN",
    "PostCode": "46143",
    "Phone": "(888) 341-5646"
},
{
    "CompanyName": "L.F. GRAPHICS LLC",
    "StreetAddress": "Paterson, ",
    "Region": "Paterson",
    "State": "NJ",
    "PostCode": "07524",
    "Phone": "(973) 240-7033"
},
{
    "CompanyName": "Pacific Sportswear And Emblem Company",
    "StreetAddress": "San Diego, ",
    "Region": "Diego",
    "State": "CA",
    "PostCode": "92120",
    "Phone": "(619) 281-6688"
}
]
<?php
$filename = 'AK.json';
$content = file_get_contents($filename);
print_r(json_decode($content,true));
?>
并且在PHP中:

[
{
    "CompanyName": "Logo Shirts Direct",
    "StreetAddress": "1001 Commerce Parkway South Dr Suite E",
    "Region": "Greenwood",
    "State": "IN",
    "PostCode": "46143",
    "Phone": "(888) 341-5646"
},
{
    "CompanyName": "L.F. GRAPHICS LLC",
    "StreetAddress": "Paterson, ",
    "Region": "Paterson",
    "State": "NJ",
    "PostCode": "07524",
    "Phone": "(973) 240-7033"
},
{
    "CompanyName": "Pacific Sportswear And Emblem Company",
    "StreetAddress": "San Diego, ",
    "Region": "Diego",
    "State": "CA",
    "PostCode": "92120",
    "Phone": "(619) 281-6688"
}
]
<?php
$filename = 'AK.json';
$content = file_get_contents($filename);
print_r(json_decode($content,true));
?>


当我执行这个脚本时,什么也没有发生,我还尝试了
gettype()
函数来获取它返回的变量的类型
NUll

看起来你的AK.json有一个BOM表。你可以用它来测试

$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 === strncmp($content, $bom, 3)) {
    echo "BOM detected - file is UTF-8\n";
    $str = substr($content, 3);
}


您能做到:

$filename = './AK.json';
$content = file_get_contents($filename);
if($content === false){ 
  echo 'something wrong here';
}else{
  print_r(json_decode($content,true));
}
我认为问题在于你的ressource AK.json的路径,确保他在正确的位置:)

试试这个

<?php
$data='
[
{
    "CompanyName": "Logo Shirts Direct",
    "StreetAddress": "1001 Commerce Parkway South Dr Suite E",
    "Region": "Greenwood",
    "State": "IN",
    "PostCode": "46143",
    "Phone": "(888) 341-5646"
},
{
    "CompanyName": "L.F. GRAPHICS LLC",
    "StreetAddress": "Paterson, ",
    "Region": "Paterson",
    "State": "NJ",
    "PostCode": "07524",
    "Phone": "(973) 240-7033"
},
{
    "CompanyName": "Pacific Sportswear And Emblem Company",
    "StreetAddress": "San Diego, ",
    "Region": "Diego",
    "State": "CA",
    "PostCode": "92120",
    "Phone": "(619) 281-6688"
}
]';
echo '<pre>';
print_r(json_decode($data,true));
echo '</pre>';

当我尝试在没有
json_decode
函数的情况下打印它时,它会打印出字符串,我想在jsonit中处理它返回
string(214)”[{“公司名称”:“Logo Shirts Direct”,“StreetAddress”:“1001商业大道南部Dr Suite E”,“Region”:“Greenwood”,“State”:“in”,“PostCode”:“46143”,“Phone”:(888)341-5646”}]“
我的PHP版本或某些扩展有问题吗?我想您的AK.json中有一个BOM表。您可以使用
$bom=pack(“CCC”,0xef,0xbb,0xbf)对其进行测试;如果(0===strncmp($content,$bom,3)){echo“检测到bom-文件是UTF-8\n;$str=substr($content,3);}
bom-检测到文件是UTF-8,这是什么?如何消除这种情况?给定的路径很好,因为当我在没有
json\u decode
函数的情况下回显内容时,它会打印出字符串数据。但是当我应用
json_decode
时,它会变为空,就像@Gabscap说它可能是一个BOM表一样,要删除它,请继续工作,但我需要不在同一文件中的内容检查该文件中包含的任何空格或字符。如果包含字符,请删除该文件