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

时间:2015-07-22 09:03:32

标签: php mysql json

我试图从JSON文件中获取内容,使用PHP将其放入MySQL数据库,这是我到目前为止的尝试方式。

这是JSON文件.wpcf7 .wpcf7-list-item:checked .wpcf7-list-item-label:before{ content:'\2714'; text-indent: .9em; color: #9CE2AE; background-color: #4DCB6D; }

AK.json

,在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()

3 个答案:

答案 0 :(得分:2)

好像你的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);
}

Here is how you get rid of BOMWhat is BOM?

Source: PHP Snippet

答案 1 :(得分:0)

你能做到:

$filename = './AK.json';
$content = file_get_contents($filename);
if($content === false){ 
  echo 'something wrong here';
}else{
  print_r(json_decode($content,true));
}

我认为问题是你的资源AK.json的路径,确保他在正确的地方:)

答案 2 :(得分:0)

试试这个

<?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>';

如果它正常工作,那么你的AK.json文件包含[]

之外的任何字符