将 JSON 对象写入服务器上的 .json 文件
我正在尝试将 JSON 对象写入服务器上的 .json 文件。我现在这样做的方式是:
JavaScript:
function createJsonFile() {
var jsonObject = {
"metros" : [],
"routes" : []
};
// write cities to JSON Object
for ( var index = 0; index < graph.getVerticies().length; index++) {
jsonObject.metros[index] = JSON.stringify(graph.getVertex(index).getData());
}
// write routes to JSON Object
for ( var index = 0; index < graph.getEdges().length; index++) {
jsonObject.routes[index] = JSON.stringify(graph.getEdge(index));
}
// some jQuery to write to file
$.ajax({
type : "POST",
url : "json.php",
dataType : 'json',
data : {
json : jsonObject
}
});
};
PHP:
<?php
$json = $_POST['json'];
$info = json_encode($json);
$file = fopen('new_map_data.json','w+');
fwrite($file, $info);
fclose($file);
?>
它写得很好,信息似乎是正确的,但它没有正确呈现。它的结果是:
{"metros":["{\\\"code\\\":\\\"SCL\\\",\\\"name\\\":\\\"Santiago\\\",\\\"country\\\":\\\"CL\\\",\\\"continent\\\":\\\"South America\\\",\\\"timezone\\\":-4,\\\"coordinates\\\":{\\\"S\\\":33,\\\"W\\\":71},\\\"population\\\":6000000,\\\"region\\\":1}",
...但我期待的是:
"metros" : [
{
"code" : "SCL" ,
"name" : "Santiago" ,
"country" : "CL" ,
"continent" : "South America" ,
"timezone" : -4 ,
"coordinates" : {"S" : 33, "W" : 71} ,
"population" : 6000000 ,
"region" : 1
} ,
为什么我会得到所有这些斜线以及为什么它们都在一行上?
I'm trying to write my JSON object to a .json file on the server. The way I'm doing this now is:
JavaScript:
function createJsonFile() {
var jsonObject = {
"metros" : [],
"routes" : []
};
// write cities to JSON Object
for ( var index = 0; index < graph.getVerticies().length; index++) {
jsonObject.metros[index] = JSON.stringify(graph.getVertex(index).getData());
}
// write routes to JSON Object
for ( var index = 0; index < graph.getEdges().length; index++) {
jsonObject.routes[index] = JSON.stringify(graph.getEdge(index));
}
// some jQuery to write to file
$.ajax({
type : "POST",
url : "json.php",
dataType : 'json',
data : {
json : jsonObject
}
});
};
PHP:
<?php
$json = $_POST['json'];
$info = json_encode($json);
$file = fopen('new_map_data.json','w+');
fwrite($file, $info);
fclose($file);
?>
It is writing fine and the information seems to be correct, but it is not rendering properly. It is coming out as:
{"metros":["{\\\"code\\\":\\\"SCL\\\",\\\"name\\\":\\\"Santiago\\\",\\\"country\\\":\\\"CL\\\",\\\"continent\\\":\\\"South America\\\",\\\"timezone\\\":-4,\\\"coordinates\\\":{\\\"S\\\":33,\\\"W\\\":71},\\\"population\\\":6000000,\\\"region\\\":1}",
... but I'm expecting this:
"metros" : [
{
"code" : "SCL" ,
"name" : "Santiago" ,
"country" : "CL" ,
"continent" : "South America" ,
"timezone" : -4 ,
"coordinates" : {"S" : 33, "W" : 71} ,
"population" : 6000000 ,
"region" : 1
} ,
Why am I getting all of these slashes and why it is all on one line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你是双重编码。不需要用JS和PHP编码,只需要一侧进行,一次就可以了。
请注意,
dataType
参数表示预期的响应类型,而不是您发送数据的类型。默认情况下,Post 请求将作为application/x-www-form-urlencoded
发送。我认为您根本不需要该参数。您可以将其缩减为:
然后(在 PHP 中)执行以下操作:
You are double-encoding. There is no need to encode in JS and PHP, just do it on one side, and just do it once.
Note that the
dataType
parameter denotes the expected response type, not the the type you send the data as. Post requests will be sent asapplication/x-www-form-urlencoded
by default.I don't think you need that parameter at all. You could trim that down to:
Then (in PHP) do:
不要
JSON.stringify
。通过这样做,您可以获得双重 JSON 编码。首先将数组元素转换为 JSON 字符串,然后将它们添加到完整对象中,然后对大对象进行编码,但是在编码时,已编码的元素将被视为简单字符串,因此所有特殊字符都会被转义。您需要有一个大对象并只对其进行一次编码。编码器将照顾孩子们。
对于行问题,请尝试发送 JSON 数据类型标头:
Content-type: text/json
我认为(没有谷歌搜索它) 。但渲染仅取决于您的浏览器。也可以使用缩进进行编码。Don't
JSON.stringify
. You get a double JSON encoding by doing that.You first convert your array elements to a JSON string, then you add them to your full object, and then you encode your big object, but when encoding the elements already encoded are treated as simple strings so all the special chars are escaped. You need to have one big object and encode it just once. The encoder will take care of the children.
For the on row problem try sending a JSON data type header:
Content-type: text/json
I think (didn't google for it). But rendering will depend only on your browser. Also it may be possible to encode with indentation.可能来不及回答这个问题了。但我遇到了同样的问题。我通过使用“JSON_PRETTY_PRINT”解决了这个问题,
以下是我的代码:
Probably too late to answer the question. But I encountered the same issue. I resolved it by using "JSON_PRETTY_PRINT"
Following is my code:
数据.json
data.json