繁体   English   中英

PHP-JSON字符串的json_decode()为null

[英]PHP - json_decode() of a JSON string is null

我在浏览器中呼叫此行:

example.com/save.php?params={"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"j@doe.com","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。 我需要像对User.json文件( $jsonStr )一样,将$getJSON字符串解码为PHP数组,但是它不起作用。

我究竟做错了什么?

非常感谢!

这不是JSON字符串,但您不会正确地urlencode()查询字符串。 die(urlencode('{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"j@doe.com","followedBy":["john","sarah"]}'));

^将上述PHP输出附加为params=

而正确的是,到POSTJSON作为内容。

看来$ getJson为空。

使用var_dump函数或print_r函数检查其值。

var_dump($getJson); // see what is the output. 

PHP文档指出,如果无法解码字符串,则json_decode()返回NULL。 最有可能的原因是语法错误,原因是网址编码错误。

为什么首先要对json参数进行json编码? URL编码是在URL中传递参数的公认方法。

编辑:

print_r($updateArr);

显示正确的json,因此问题出在其他地方

您的代码还有其他几个问题。

  1. echo 'UPDATE_ARR: ' .$updateArr; 引发通知:第6行中的数组到字符串的转换。根据您的PHP版本和配置,由于echo中错误的类型转换,可能会打印NULL。
  2. 警告:array_push()期望参数1为数组,第12行给出null。检查users.json文件是否存在并且正确。

我已经更新了您的代码并在我这边对其进行了测试。 请尝试一下。

  <?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,“电子邮件”:“ j@doe.com”,“ followedBy”:[“ john”,“ sarah”] } UPDATE_ARR:数组([objID] => i8O0FRuGEr [用户名] => johndoe [密码] => 123456 [电子邮件] => j@doe.com [followedBy] =>数组([0] =>约翰[1] = > sarah))JSON数据:{“ ID”:“ i8O0FRuGEr”,“ user_name”:“ johndoe”,“ pass”:123456,“ email_id”:“ j@doe.com”,“ followed_By”:[“ john” “萨拉”], “0”:{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]}, “1”:空, “2”:{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”:「J @ doe.com”, “followedBy”:[ “约翰”, “萨拉”]} “3”:{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件” : “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} “4”:{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456 , “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} “5”:{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”,”密码“:123456,”电子邮件“:” j@doe.com “ ”followedBy“:[ ”约翰“, ”萨拉“]} ”6“:{” OBJID “:” i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} “7” :{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} , “8”:{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”,”莎拉”]}}

使用完users.json文件后:

[{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} ,{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} ,{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} ,{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} ,{ “OBJID”: “i8O0FRuGEr”, “用户名”: “输入johndoe”, “密码”:123456, “电子邮件”: “j@doe.com”, “followedBy”:[ “约翰”, “萨拉”]} ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM