php PHP从文件读取和写入JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8858848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP read and write JSON from file
提问by Josiah
I have the following JSON in a file list.txt
:
我在文件中有以下 JSON list.txt
:
{
"bgates":{"first":"Bill","last":"Gates"},
"sjobs":{"first":"Steve","last":"Jobs"}
}
How do I add "bross":{"first":"Bob","last":"Ross"}
to my file using PHP?
如何"bross":{"first":"Bob","last":"Ross"}
使用 PHP添加到我的文件中?
Here's what I have so far:
这是我到目前为止所拥有的:
<?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));
?>
Which gives me a Fatal error: Cannot use object of type stdClass as array on this line:
这给了我一个致命错误:不能在这一行使用 stdClass 类型的对象作为数组:
$json[$user] = array("first" => $first, "last" => $last);
I'm using PHP5.2. Any thoughts? Thanks!
我正在使用 PHP5.2。有什么想法吗?谢谢!
回答by John Carter
The clue is in the error message - if you look at the documentation for json_decode
note that it can take a second param, which controls whether it returns an array or an object - it defaults to object.
线索在错误消息中 - 如果您查看文档以json_decode
注意它可以采用第二个参数,该参数控制它是返回数组还是对象 - 它默认为 object。
So change your call to
所以改变你的电话
$json = json_decode(file_get_contents($file), true);
And it'll return an associative array and your code should work fine.
它会返回一个关联数组,您的代码应该可以正常工作。
回答by 4EACH
The sample for reading and writing JSON in PHP:
PHP读写JSON示例:
$json = json_decode(file_get_contents($file),TRUE);
$json[$user] = array("first" => $first, "last" => $last);
file_put_contents($file, json_encode($json));
回答by Liam Bailey
回答by Aknosis
You need to make the decode function return an array by passing in the true
parameter.
您需要通过传入true
参数使 decode 函数返回一个数组。
json_decode(file_get_contents($file),true);
json_decode(file_get_contents($file),true);
回答by Lubor Bílek
Try using second parameter for json_decode function:
尝试使用 json_decode 函数的第二个参数:
$json = json_decode(file_get_contents($file), true);
回答by Ur Friend
This should work for you to get the contents of list.txt
file
这应该适合您获取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);
回答by Ur Friend
If you want to display the JSON data in well defined formate you can modify the code as:
如果要以定义良好的格式显示 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";
}
回答by markllibre
When you want to create json format it had to be in this format for it to read:
当你想创建 json 格式时,它必须是这种格式才能读取:
[
{
"":"",
"":[
{
"":"",
"":""
}
]
}
]