如何用 PHP 生成 .json 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2467945/
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
How to generate .json file with PHP?
提问by Egglabs
CREATE TABLE Posts
{
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(200),
url VARCHAR(200)
}
json.php code
json.php 代码
<?php
$sql=mysql_query("select * from Posts limit 20");
echo '{"posts": [';
while($row=mysql_fetch_array($sql))
{
$title=$row['title'];
$url=$row['url'];
echo '
{
"title":"'.$title.'",
"url":"'.$url.'"
},';
}
echo ']}';
?>
I have to generate results.jsonfile.
我必须生成results.json文件。
回答by Alec Smart
Here is a sample code:
这是一个示例代码:
<?php
$sql="select * from Posts limit 20";
$response = array();
$posts = array();
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)) {
$title=$row['title'];
$url=$row['url'];
$posts[] = array('title'=> $title, 'url'=> $url);
}
$response['posts'] = $posts;
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
?>
回答by RileyManda
Use this:
用这个:
$json_data = json_encode($posts);
file_put_contents('myfile.json', $json_data);
You have to create the myfile.json before you run the script.
您必须在运行脚本之前创建 myfile.json。
回答by chelmertz
Insert your fetched values into an array instead of echoing.
将获取的值插入数组而不是回显。
Use file_put_contents()and insert json_encode($rows)into that file, if $rowsis your data.
如果是您的数据,请使用file_put_contents()并插入json_encode($rows)到该文件中$rows。
回答by Pasupathi Thangavel
Here i have mentioned the simple syntex for create json fileand print the array value inside the jsonfile in pretty manner.
在这里,我提到了用于创建json 文件并以漂亮的方式在json文件中打印数组值的简单语法。
$array = array('name' => $name,'id' => $id,'url' => $url);
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($array, JSON_PRETTY_PRINT)); // here it will print the array pretty
fclose($fp);
Hope it will works for you....
希望它对你有用......
回答by Quentin
Use PHP's json methodsto create the json then write it to a file with fwrite.
回答by Sarfraz
You can simply use json_encodefunction of php and save file with file handling functions such as fopenand fwrite.
您可以简单地使用php 的json_encode函数并使用fopen和fwrite等文件处理函数保存文件。
回答by CyberJunkie
If you're pulling dynamic records it's better to have 1 php file that creates a json representation and not create a file each time.
如果您要提取动态记录,最好有 1 个 php 文件来创建 json 表示,而不是每次都创建一个文件。
my_json.php
my_json.php
$array = array(
'title' => $title,
'url' => $url
);
echo stripslashes(json_encode($array));
Then in your script set the path to the file my_json.php
然后在您的脚本中设置文件的路径 my_json.php
回答by Darkcoder
First, you need to decode it :
首先,您需要对其进行解码:
$jsonString = file_get_contents('jsonFile.json');
$data = json_decode($jsonString, true);
Then change the data :
然后更改数据:
$data[0]['activity_name'] = "TENNIS";
// or if you want to change all entries with activity_code "1"
foreach ($data as $key => $entry) {
if ($entry['activity_code'] == '1') {
$data[$key]['activity_name'] = "TENNIS";
}
}
Then re-encode it and save it back in the file:
然后重新编码并将其保存回文件中:
$newJsonString = json_encode($data);
file_put_contents('jsonFile.json', $newJsonString);

