【问题标题】:PHP read and write JSON from filePHP从文件中读取和写入JSON
【发布时间】:2017-02-15 20:21:40
【问题描述】:

我在文件 list.txt 中有以下 JSON:

{
"bgates":{"first":"Bill","last":"Gates"},
"sjobs":{"first":"Steve","last":"Jobs"}
}

如何使用 PHP 将"bross":{"first":"Bob","last":"Ross"} 添加到我的文件中?

这是我目前所拥有的:

<?php

$user = "bross";
$first = "Bob";
$last = "Ross";

$file = "list.txt";

$json = json_decode(file_get_contents($file));

$json[$user] = array("first" => $first, "last" => $last);

file_put_contents($file, json_encode($json));

?>

这给了我一个致命错误:不能在这一行使用 stdClass 类型的对象作为数组:

$json[$user] = array("first" => $first, "last" => $last);

我正在使用 PHP5.2。有什么想法吗?谢谢!

【问题讨论】:

    标签: php json


    【解决方案1】:

    线索在错误消息中 - 如果您查看 json_decode 的文档,请注意它可以采用第二个参数,该参数控制返回数组还是对象 - 它默认为对象。

    所以把你的电话改成

    $json = json_decode(file_get_contents($file), true);
    

    它会返回一个关联数组,您的代码应该可以正常工作。

    【讨论】:

      【解决方案2】:

      PHP读写JSON示例:

      $json = json_decode(file_get_contents($file),TRUE);
      
      $json[$user] = array("first" => $first, "last" => $last);
      
      file_put_contents($file, json_encode($json));
      

      【讨论】:

      • json_encode的第二个参数不是布尔值,所以写json_encode($json, TRUE)是错误的。
      • 根据文档 (php.net/manual/en/function.json-decode.php),第二个参数是布尔值。
      • @PedroLobito - 我正在查看 4EACH 给出的代码。第二个参数作为布尔值使用的唯一函数是 json_decode。 json_encode 稍后使用,但没有第二个参数。当然我可以看到作者编辑了代码。不知道该编辑是否是为了更正该功能的使用。
      【解决方案3】:

      或者只使用 $json 作为对象:

      $json->$user = array("first" => $first, "last" => $last);
      

      这是在没有第二个参数的情况下返回的方式(作为stdClass 的实例)。

      【讨论】:

        【解决方案4】:

        您需要通过传入true参数使解码函数返回一个数组。

        json_decode(file_get_contents($file),true);

        【讨论】:

          【解决方案5】:

          尝试使用 json_decode 函数的第二个参数:

          $json = json_decode(file_get_contents($file), true);
          

          【讨论】:

            【解决方案6】:

            这应该可以让您获取list.txt 文件的内容

            $headers = array('http'=>array('method'=>'GET','header'=>'Content: type=application/json \r\n'.'$agent \r\n'.'$hash'));
            
            $context=stream_context_create($headers);
            
            $str = file_get_contents("list.txt",FILE_USE_INCLUDE_PATH,$context);
            
            $str=utf8_encode($str);
            
            $str=json_decode($str,true);
            
            print_r($str);
            

            【讨论】:

            • 我只是得到 Array 作为我的输出
            【解决方案7】:

            如果您想以明确定义的格式显示 JSON 数据,您可以将代码修改为:

            file_put_contents($file, json_encode($json,TRUE));
            
            
            $headers = array('http'=>array('method'=>'GET','header'=>'Content: type=application/json \r\n'.'$agent \r\n'.'$hash'));
            
            $context=stream_context_create($headers);
            
            $str = file_get_contents("list.txt",FILE_USE_INCLUDE_PATH,$context);
            
            $str1=utf8_encode($str);
            
            $str1=json_decode($str1,true);
            
            
            
            foreach($str1 as $key=>$value)
            {
            
                echo "key is: $key.\n";
            
                echo "values are: \t";
                    foreach ($value as $k) {
            
                    echo " $k. \t";
                    # code...
                }
                    echo "<br></br>";
                    echo "\n";
            
            }
            

            【讨论】:

              【解决方案8】:

              当你想创建 json 格式时,它必须是这种格式才能读取:

              [ 
                {
                  "":"",
                  "":[
                   {
                     "":"",
                     "":""
                   }
                  ]
                }
              ]
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2020-05-13
                • 2021-04-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多