PHP从文件读取和写入JSON

PHP从文件读取和写入JSON,php,json,Php,Json,我在文件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.

我在文件
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));

?>

我使用的是PHP5.2。有什么想法吗?谢谢

线索在错误消息中-如果您查看文档以了解它可以接受第二个参数的注意事项,该参数控制它是返回数组还是返回对象-它默认为object

所以把你的电话改成

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

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

您需要让decode函数通过传入
true
参数返回数组


json\u解码(file\u get\u contents($file),true)

尝试为json_解码函数使用第二个参数:

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

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

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

这是在不使用第二个参数(作为的实例)的情况下返回的方式。

这对于获取
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);

在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格式时,必须使用此格式才能读取:

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

如果希望以定义良好的格式显示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";

}
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\u context\u create($headers);
$str=file\u get\u contents(“list.txt”,file\u USE\u INCLUDE\u PATH,$context);
$str1=utf8\U编码($str);
$str1=json_decode($str1,true);
foreach($str1作为$key=>$value)
{
echo“key是:$key.\n”;
echo“值为:\t”;
foreach(价值为$k){
回显“$k.\t”;
#代码。。。
}
回声“

”; 回音“\n”; }
我只是将数组作为我的输出,json_encode的第二个参数不是布尔值,所以写
json_encode($json,TRUE)
是错误的。根据文档()第二个参数是布尔值。@pedrobito-我正在看4EACH给出的代码。在这里,作为布尔值与第二个参数一起使用的唯一函数是json_decode。稍后使用json_encode,但没有第二个参数。当然我可以看到作者编辑了代码。未知该编辑是否用于更正函数的使用。
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";

}