将数据从 PHP 文件写入 json 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17623550/
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
Write data to a json file from PHP file
提问by user2521326
I have a test.php page which displayes three has 3 "Add Link" buttons, on clicking the buttons the user sees a popup window. In the window he adds the link. Once the link is added , the base page will change from "Add link" button to hyperlink with the new link. Now, I have to pass the new link I receive from the user from test.php to links.php using an ajax call. Links.php has to have a JSON code to write the link to another file called first.json. first.jason will have key value pair for variable and link. I would have to retrieve the value from .json file later and reconstruct into an array, update the corresponding variable and save it back.
我有一个 test.php 页面,它显示三个有 3 个“添加链接”按钮,单击按钮时用户会看到一个弹出窗口。在窗口中,他添加了链接。添加链接后,基本页面将从“添加链接”按钮更改为带有新链接的超链接。现在,我必须使用 ajax 调用将我从用户那里收到的新链接从 test.php 传递到 links.php。Links.php 必须有一个 JSON 代码才能将链接写入另一个名为 first.json 的文件。first.jason 将具有变量和链接的键值对。稍后我必须从 .json 文件中检索值并重建为一个数组,更新相应的变量并将其保存回来。
I have by far, managed to get the new link from test.php and able to send the same via ajax call to links.php. I am also able to display the link I receive and have verified the same. Now, I would like to copy the link into .json file as a key vale pair. I am new to json and unable to figure out how to go about it. My variable $p, in links.php has the link.
到目前为止,我已经设法从 test.php 获取新链接,并能够通过 ajax 调用将相同的链接发送到 links.php。我也能够显示我收到的链接并已验证相同。现在,我想将该链接作为键值对复制到 .json 文件中。我是 json 的新手,无法弄清楚如何去做。我的变量 $p,在 links.php 中有链接。
Any pointers on the same will be helpful. Thanks.
任何相同的指针都会有所帮助。谢谢。
Below is my code in test.php:
下面是我在 test.php 中的代码:
<!DOCTYPE html>
<html>
<body>
<div id="demos1">
<button id="demo1" onclick="Link1()">Add Link-1</button>
<br>
</div>
<div id="demos2">
<button id="demo2" onclick="Link2()">Add Link-2</button>
<br>
</div>
<div id="demos3">
<button id="demo3" onclick="Link3()">Add Link-3</button>
<br>
</div>
<div id="txtHint"></div>
<script>
function Link1()
{
var demo1 = document.getElementById('demo1');
var demos1 = document.getElementById('demos1');
var value1 = prompt("Please Enter the Link");
var link1 = document.createElement('a');
link1.setAttribute('href', value1);
link1.innerHTML = "New Link1";
demo1.parentNode.removeChild(demo1);
demos1.appendChild(link1);
sendlink(value1);
}
function Link2()
{
var demo2 = document.getElementById('demo2');
var demos2 = document.getElementById('demos2');
var value2 = prompt("Please Enter the Link");
var link2 = document.createElement('a');
link2.setAttribute('href', value2);
link2.innerHTML = "New Link2";
demo2.parentNode.removeChild(demo2);
demos2.appendChild(link2);
sendlink(value2);
}
function Link3()
{
var demo3 = document.getElementById('demo3');
var demos3 = document.getElementById('demos3');
var value3 = prompt("Please Enter the Link");
var link3 = document.createElement('a');
link3.setAttribute('href', value3);
link3.innerHTML = "New Link3";
demo3.parentNode.removeChild(demo3);
demos3.appendChild(link3);
sendlink(value3);
}
function sendlink(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="hello";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","links.php?q="+str,true);
xmlhttp.send();
}
</script>
</body>
</html>
Below is the code for links.php which receives the value(i.e, link) the test.php sends through ajax call:
下面是 links.php 的代码,它接收 test.php 通过 ajax 调用发送的值(即链接):
<?php
include 'test.php';
$p=$_REQUEST['q'];
?>
I am able to write to json file using json_encode. Now I would have to read the link from .json file, associate it to the corresponding variable and save it back. How would I go about this?
我可以使用 json_encode 写入 json 文件。现在我必须从 .json 文件中读取链接,将其关联到相应的变量并将其保存回来。我该怎么办?
回答by D-Rock
To write the json:
编写json:
file_put_contents('filename.json', json_encode($p));
To read the json:
读取json:
$p = json_decode(file_get_contents('filename.json'));
Obviously this does no error checking at all though.
显然,这根本没有错误检查。
回答by Ravi Mane
if you want to insert the data in json file below one is usefull
如果你想在json文件中插入数据,下面一个很有用
function exportToJson() {
mysql_connect("localhost", "root", "");
mysql_select_db("krasimir_benchmark");
$res = mysql_query("SELECT * FROM users ORDER BY id");
$records = array();
while($obj = mysql_fetch_object($res)) {
$records []= $obj;
}
file_put_contents("data.json", json_encode($records));
}
回答by ZeroBryan
If you have created your json already, then you can simply do this:
如果您已经创建了 json,那么您可以简单地执行以下操作:
fwrite("yourjson.json",json_encode($yourvariablewithdata))
If you have NOT created your json file, then you could create it with fopen function. See details http://www.php.net/manual/es/function.fopen.php
如果您尚未创建 json 文件,则可以使用 fopen 函数创建它。查看详情http://www.php.net/manual/es/function.fopen.php
回答by Asinus Rex
You can always JSON.stringify the JSON on the client side with jQuery, like so:
您始终可以使用 jQuery 在客户端对 JSON 进行 JSON.stringify,如下所示:
var dataForJson = JSON.stringify(your_aray);
Then on the server side write it as is (no need for json_encode):
然后在服务器端按原样写(不需要 json_encode):
$jsondata = $_POST['dataForJson'];
$writeJson = file_put_contents('data_en.json', $jsondata);

