<?php
$postcode=$_POST['script'];
// $myfile = file_get_contents("./aircondata/".$postcode.".json");
// echo $myfile;
$myfile = '{
"1": { "Brand":"MITSUBISHI HEAVY INDUSTRIES, LTD.", "Model_No":"SRK20ZMXA-S / SRC20ZMXA-S", "C.Power_Inp_Rated":0.35, "H.Power_Inp_Rated":0.45, "KWH":1881.95, "Cost":564.585},
"2": { "Brand":"MITSUBISHI HEAVY INDUSTRIES LTD.", "Model_No":"SRK20ZIX-S", "C.Power_Inp_Rated":0.35, "H.Power_Inp_Rated":0.45, "KWH":1881.95, "Cost":564.585},
"3": { "Brand":"MITSUBISHI HEAVY INDUSTRIES LTD.", "Model_No":"SRK20ZJX-S", "C.Power_Inp_Rated":0.35, "H.Power_Inp_Rated":0.45, "KWH":1881.95, "Cost":564.585},
"4": { "Brand":"TOSHIBA", "Model_No":"RAS-07PKVP-E/PAVP-E", "C.Power_Inp_Rated":0.355, "H.Power_Inp_Rated":0.44, "KWH":1908.835, "Cost":572.6505},
"5": { "Brand":"CARRIER", "Model_No":"50PCH006", "C.Power_Inp_Rated":0.4, "H.Power_Inp_Rated":0.4, "KWH":2150.8, "Cost":645.24}
}';
$ac_decoded = json_decode($myfile, true);
echo $ac_decoded;
foreach ($ac_decoded as $key => $value) {
if($key=="1"){
echo $value["Brand"];
}
}
echo "okay";
fclose($myfile);
?>
我的问题是,上面的代码在json数组存在于同一个.php文件中时有效,但注释掉的代码(使用file_get_contents)对json_decode不起作用,只有在回显时才有效。 请帮忙......谢谢!!
要回答第一个回复中的建议,以下是.json
文件,不确定格式是否正确
'{
"1": { "Brand":"MITSUBISHI HEAVY INDUSTRIES, LTD.", "Model_No":"SRK20ZMXA-S / SRC20ZMXA-S", "C.Power_Inp_Rated":0.35, "H.Power_Inp_Rated":0.45, "KWH":1881.95, "Cost":564.585},
"2": { "Brand":"MITSUBISHI HEAVY INDUSTRIES LTD.", "Model_No":"SRK20ZIX-S", "C.Power_Inp_Rated":0.35, "H.Power_Inp_Rated":0.45, "KWH":1881.95, "Cost":564.585},
"3": { "Brand":"MITSUBISHI HEAVY INDUSTRIES LTD.", "Model_No":"SRK20ZJX-S", "C.Power_Inp_Rated":0.35, "H.Power_Inp_Rated":0.45, "KWH":1881.95, "Cost":564.585},
"4": { "Brand":"TOSHIBA", "Model_No":"RAS-07PKVP-E/PAVP-E", "C.Power_Inp_Rated":0.355, "H.Power_Inp_Rated":0.44, "KWH":1908.835, "Cost":572.6505},
"5": { "Brand":"CARRIER", "Model_No":"50PCH006", "C.Power_Inp_Rated":0.4, "H.Power_Inp_Rated":0.4, "KWH":2150.8, "Cost":645.24}
}'
答案 0 :(得分:0)
参考我现有的Answer ..您在file_get_contents
电话中有语法:
$myfile = file_get_contents("./aircondata/".$postcode.".json");
您必须转义特殊字符,如下所示:
$myfile = file_get_contents(".//aircondata//".$postcode.".json");
P.S。:你一直关注我认为在你的服务器上没有活动的php错误报告。
<强>更新强>
另一个可能的原因可能是所包含文件的JSON内容的格式,请确保validate the contents。
如果该文件中的数据确实用单引号括起来,那么这是一个无效的JOSN
文件。