[英]json_decode returning an array of 1
I am trying to decode some JSON into a php array. 我正在尝试将JSON解码为php数组。 Here's the code excerpt: 这是代码摘录:
$getfile="{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}";
$arr = json_decode($getfile, true);
$arr['day3'] = $selecter;
echo(print_r($arr));
The only thing that gets returned is '1'. 返回的唯一内容是“ 1”。 I've checked JSONLint and it is valid json, so I'm wondering why json_decode is failing. 我检查了JSONLint,它是有效的json,所以我想知道json_decode为何失败。 I also tried checking what the array is before adding the day3 key to it, and I still return a '1'. 我还尝试在将day3键添加到该数组之前检查该数组是什么,但仍然返回“ 1”。 Any help is appreciated! 任何帮助表示赞赏!
Actual code: 实际代码:
$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");
}
These are the contents of $file (it's a text file) 这些是$ file的内容(它是一个文本文件)
{"fname":"Bob","lname":"Thomas","cascade":"bthomas","loc":"res","place":"home 2"}
We still don't know what's in your file. 我们仍然不知道您文件中的内容。 However if: 但是如果:
"{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}"
Then the extraneous outer double quotes will screw the JSON , and json_decode
will return NULL. 然后,多余的外部双引号将拧紧JSON ,而json_decode
将返回NULL。 Use json_last_error()
to find out. 使用json_last_error()
找出json_last_error()
。 Might also be a UTF-8 BOM or something else ... 也可能是UTF-8 BOM或其他东西...
Anyway, the 1
is the result from print_r
. 无论如何, 1
是print_r
的结果。 print_r outputs directly, you don't need the echo. print_r直接输出,不需要回显。 Also for debugging rather use var_dump()
也用于调试而不是使用var_dump()
More specifically you would want the print_r
output returned (instead of the boolean success result 1
) and then write that to the log. 更具体地说,您希望返回print_r
输出(而不是布尔成功结果1
),然后将其写入日志。
So use: 因此使用:
writeLog(print_r($arr, TRUE));
Notice the TRUE
parameter. 注意TRUE
参数。
First, use a single quote. 首先,使用单引号。 That will cause a parse error 这将导致解析错误
$getfile= '{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}';
I assume you have already declared $selecter and it has been assigned to some value. 我假设您已经声明了$ selecter,并且它已经分配了一些值。
Remove echo from echo(print_r($arr))
You don't need echo. 从echo(print_r($arr))
删除echo不需要echo。 print_r will also output. print_r也将输出。 If you use echo, it will display 1 at the end of array. 如果使用echo,它将在数组末尾显示1。
The working code: 工作代码:
$getfile = '{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}';
$arr = json_decode($getfile, true);
$selecter = 'foobar';
$arr['day3'] = $selecter;
print_r($arr);
Hope this helps. 希望这可以帮助。
I had this problem when doing it like this: 这样做时遇到了这个问题:
if ($arr = json_decode($getfile, true)) {
$arr['day3'] = $selecter;
echo(print_r($arr));
}
Decoding it outside of if was the solution. 如果解决方案之外,则将其解码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.