我在浏览器中呼叫此行:
example.com/save.php?params={"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}
这是我的save.php代码:
<?php
include 'Config.php';
$getJSON = $_GET['params'];
echo 'getJSON: '.$getJSON.'<br><br>';
$updateArr = json_decode($getJSON, true);
echo 'UPDATE_ARR: ' .$updateArr; //<-- it prints 'null'...
$jsonStr = file_get_contents("Users.json");
// Decode the JSON string into a PHP array.
$objects = json_decode($jsonStr, true);
array_push($objects, $updateArr);
// Encode the array back into a JSON string and save it.
$jsonData = json_encode($objects);
file_put_contents("Users.json", $jsonData);
// echo data
echo 'JSON DATA: ' .$jsonData;
?>
问题是此代码使我的User.json文件为“ null”,它删除了其中的所有对象,实际上,
echo 'UPDATE_ARR: ' .$updateArr;
返回null。我需要将我的
$getJSON
字符串解码为一个PHP数组,就像对User.json文件($jsonStr
)所做的那样,但是它不起作用。我究竟做错了什么?
非常感谢!
最佳答案
我已经更新了您的代码,并在我这边对其进行了测试。请尝试一下。
<?php
include 'Config.php';
if(!empty($_GET['params'])){
$getJSON = $_GET['params'];
echo 'getJSON: '.$getJSON.'<br><br>';
$updateArr = json_decode($getJSON, true);
echo 'UPDATE_ARR: ';print_r($updateArr); //<-- it prints 'null'...
$jsonStr = file_get_contents("Users.json");
// Decode the JSON string into a PHP array.
$objects = json_decode($jsonStr, true);
array_push($objects, $updateArr);
// Encode the array back into a JSON string and save it.
$jsonData = json_encode($objects);
file_put_contents("Users.json", $jsonData);
// echo data
echo 'JSON DATA: ' .$jsonData;
}else{
echo 'Params is empty';
}
这是
users.json
文件多次刷新后的结果:getJSON:{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456,“电子邮件”:“ [email protected]”,“ followedBy”:[“ john”,“ sarah”] }
UPDATE_ARR:数组([objID] => i8O0FRuGEr [用户名] => johndoe [密码] => 123456 [电子邮件] => [email protected] [followedBy] =>数组([0] =>约翰[1] => sarah))JSON数据:{“ ID”:“ i8O0FRuGEr”,“ user_name”:“ johndoe”,“ pass”:123456,“ email_id”:“ [email protected]”,“ followed_By”:[[“ john”, “ sarah”],“ 0”:{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456,“电子邮件”:“ [email protected]”,“ followedBy”:[“ john“,” sarah“]},” 1“:null,” 2“:{” objID“:” i8O0FRuGEr“,”用户名“:” johndoe“,”密码“:123456,”电子邮件“:” j @ doe .com“,” followedBy“:[” john“,” sarah“]},” 3“:{” objID“:” i8O0FRuGEr“,”用户名“:” johndoe“,”密码“:123456,”电子邮件“: “ [email protected]”,“ followedBy”:[“ john”,“ sarah”]},“ 4”:{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456, “ email”:“ [email protected]”,“ followedBy”:[“ john”,“ sarah”]},“ 5”:{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码“:123456,”电子邮件“:” [email protected]“,” followedBy“:[” john“,” sarah“]},” 6“:{” objID“:” i8O0FRuGEr“,”用户名“:” johndoe “,”密码“:123456,”电子邮件“:” [email protected]“,” followedBy“:[” john“,” sarah“]},” 7“:{” objID“:” i8 O0FRuGEr“,”用户名“:” johndoe“,”密码“:123456,”电子邮件“:” [email protected]“,” followedBy“:[” john“,” sarah“]},” 8“:{” objID“:” i8O0FRuGEr“,”用户名“:” johndoe“,”密码“:123456,” email“:” [email protected]“,” followedBy“:[” john“,” sarah“]}}
使用完users.json文件后:
[{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456,“电子邮件”:“ [email protected]”,“ followedBy”:[“ john”,“ sarah”]} ,{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456,“电子邮件”:“ [email protected]”,“ followedBy”:[“ john”,“ sarah”]} ,{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456,“电子邮件”:“ [email protected]”,“ followedBy”:[“ john”,“ sarah”]} ,{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456,“电子邮件”:“ [email protected]”,“ followedBy”:[“ john”,“ sarah”]} ,{“ objID”:“ i8O0FRuGEr”,“用户名”:“ johndoe”,“密码”:123456,“电子邮件”:“ [email protected]”,“ followedBy”:[“ john”,“ sarah”]} ]