[英]Php mysql to json output formatting
I have my json echo working 我的json echo工作正常
echo '{"detailsjson":'.json_encode($var).'}';
Now I want to write this to a file on server example output.json 现在我想将此写入服务器示例output.json上的文件中
I use the php code 我使用php代码
$json_data = {"detailsjson":.json_encode($var).};
file_put_contents('output.json', $json_data);
i don't get the file, but for 我没有文件,但是
file_put_contents("output.json", json_encode($var));
it work. 这行得通。 I want to include detailsjson in my json so that i can get 我想在我的json中包含detailsjson,以便我可以
{"detailsjson":[{"Name":"Amateur : Paul Smith",.....
Thanks 谢谢
Your $json_data
variable is not "valid". 您的$json_data
变量无效。 You have to put everything in single quotes to make it work (not in double quotes because you use them in your json). 您必须将所有内容都用单引号引起来(而不是双引号,因为您在json中使用了它们)。 Like this: 像这样:
$json_data = '{"detailsjson":'.json_encode($var).'}';
Update (see comment from Hassan ): 更新(请参阅来自Hassan的评论 ):
It is better to do this with pure arrays. 最好使用纯数组来执行此操作。
$json_data = json_encode(['detailsjson' => $var]);
You can compare them by speed here (using a string) and here (using pure arrays). 您可以在此处 (使用字符串)和此处 (使用纯数组)通过速度比较它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.