我正在尝试将一些 JSON 解码为 php 数组。这是代码摘录:
$getfile="{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}";
$arr = json_decode($getfile, true);
$arr['day3'] = $selecter;
echo(print_r($arr));
唯一返回的是“1”。我检查了 JSONLint 并且它是有效的 json,所以我想知道为什么 json_decode 失败。我还尝试在向它添加 day3 键之前检查数组是什么,但我仍然返回一个“1”。任何帮助表示赞赏!
实际代码:
$getfile = "";
$getfile = file_get_contents($file);
if ($getfile == "") {
writeLog("Error reading file.");
}
writeLog("getfile is " . $getfile);
writeLog("Decoding JSON data");
$arr = json_decode($getfile, true);
writeLog("Decoded raw: " . print_r($arr));
writeLog("Editing raw data. Adding data for day " . $day);
$arr['day3'] = $selecter;
writeLog(print_r($arr));
$newfile = json_enconde($arr);
writeLog($newfile);
if (file_put_contents($file, $newfile)) {
writeLog("Wrote file to " . $file);
echo $newfile;
} else {
writeLog("Error writting file");
}
这些是 $file 的内容(它是一个文本文件)
{"fname":"Bob","lname":"Thomas","cascade":"bthomas","loc":"res","place":"home 2"}