Home  >  Article  >  Backend Development  >  javascript - json数据格式

javascript - json数据格式

WBOY
WBOYOriginal
2016-06-06 20:32:451089browse

我在服务器端用PHP写入json文件,格式 为{}{}{},用的是file_put_contents附加写入

在客户端js通过PHP获取的也是{}{}{}这种格式(file_get_contents),而不是标准的json格式[{},{},{}]

请问我该如何得到标准的json格式呢? 是在PHP写入的时候(PHP得到的只是{key/value},如何做?),还是js获取到字符串之后,再转成标准格式(如何做?)?

新手提问,thanks。

回复内容:

我在服务器端用PHP写入json文件,格式 为{}{}{},用的是file_put_contents附加写入

在客户端js通过PHP获取的也是{}{}{}这种格式(file_get_contents),而不是标准的json格式[{},{},{}]

请问我该如何得到标准的json格式呢? 是在PHP写入的时候(PHP得到的只是{key/value},如何做?),还是js获取到字符串之后,再转成标准格式(如何做?)?

新手提问,thanks。

把你要写入的内容放在 $data = array(); 中,
在写入的时候用

$data[] = $xxxxx;
$data[] = $yyyyy;
//....
file_put_contents('xx.js', json_encode($data));

然后js就可以正常加载这个 xx.js 了.

比较建议后端服务器写入文件时就是标准的JSON格式,前端拿到了就能直接解析。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn