写JSON对象.json服务器上的文件 [英] writing JSON object to .json file on server
问题描述
我试着写我的JSON对象到服务器上的.json文件。现在我这样做的方法是:
JavaScript的:
函数createJsonFile(){
VAR的JSONObject = {
地铁:[],
路线:[]
};
//写城市JSON对象
对于(VAR指数= 0;指数< graph.getVerticies()。length;在指数++){
jsonObject.metros [指数] = JSON.stringify(graph.getVertex(指数).getData());
}
//写航线JSON对象
对于(VAR指数= 0;指数< graph.getEdges()。length;在指数++){
jsonObject.routes [指数] = JSON.stringify(graph.getEdge(指数));
}
//一些jQuery写入文件
$阿贾克斯({
键入:POST,
网址:json.php
数据类型:JSON,
数据 : {
JSON:的JSONObject
}
});
};
PHP:
< PHP
$ JSON = $ _ POST ['JSON'];
$信息= json_en code($ JSON);
$文件=的fopen('new_map_data.json','W +');
FWRITE($文件,$信息);
fclose函数($文件);
?>
这是写细的信息似乎是正确的,但不能正常呈现。它是走出来的:
<$p$p><$c$c>{"metros":["{\\\"$c$c\\\":\\\"SCL\\\",\\\"name\\\":\\\"Santiago\\\",\\\"country\\\":\\\"CL\\\",\\\"continent\\\":\\\"South America\\\",\\\"timezone\\\":-4,\\\"coordinates\\\":{\\\"S\\\":33,\\\"W\\\":71},\\\"population\\\":6000000,\\\"region\\\":1}",
...但我很期待这样的:
地铁:
{
code:SCL
名:圣地亚哥,
国:瘦肉精,
大陆:南美
时区:-4,
坐标:{S:33,W:71},
人口:600万,
区域:1
},
任何想法,为什么我得到所有这些斜线以及为什么它是所有在同一行?
谢谢, 斯托伊奇
您是双编码。有没有必要EN code在JS的和的PHP,只是做在一边吧,只需做一次。
//写城市的JSON对象
对于(VAR指数= 0;指数&LT; graph.getVerticies()。length;在指数++){
/ *还没有转换成JSON这里* /
jsonObject.metros [指数] = graph.getVertex(指数).getData();
}
//写航线JSON对象
对于(VAR指数= 0;指数&LT; graph.getEdges()。length;在指数++){
/ *还没有转换成JSON这里* /
jsonObject.routes [指数] = graph.getEdge(指数);
}
//一些jQuery写入文件
$阿贾克斯({
键入:POST,
网址:json.php
数据类型:JSON,
数据 : {
JSON:JSON.stringify(的JSONObject)/ *转换这里只* /
}
});
注意的dataType
参数表示预期的响应的类型,而不是类型你发送的数据。 POST请求将始终以应用程序/ x-WWW的形式urlen codeD
。我不认为你需要的参数可言,除非你希望一个JSON响应的和的想要做的(通过的onSuccess事件处理程序IE)的东西吧。你甚至可以裁剪下来:
$后(json.php,{JSON:JSON.stringify(的JSONObject)})。
然后(在PHP)做的:
&LT; PHP
$ JSON = $ _ POST ['JSON'];
如果(json_de code($ JSON)!= NULL){/ *完整性检查* /
$文件=的fopen('new_map_data.json','W +');
FWRITE($文件,$ JSON);
fclose函数($文件);
} 其他 {
//处理错误
}
?&GT;
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
} ,
Any idea why I'm getting all of these slashes and why it is all on one line?
Thanks, Hristo
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.
// write cities to JSON Object
for ( var index = 0; index < graph.getVerticies().length; index++) {
/* do not yet convert to JSON here */
jsonObject.metros[index] = graph.getVertex(index).getData();
}
// write routes to JSON Object
for ( var index = 0; index < graph.getEdges().length; index++) {
/* do not yet convert to JSON here */
jsonObject.routes[index] = graph.getEdge(index);
}
// some jQuery to write to file
$.ajax({
type : "POST",
url : "json.php",
dataType : 'json',
data : {
json : JSON.stringify(jsonObject) /* convert here only */
}
});
Note that the dataType
parameter denotes the expected response type, instead of the type you send the data with. Post requests will always be sent as application/x-www-form-urlencoded
. I don't think you need that parameter at all, unless you expect a JSON response and want to do something with it (i.e. via the onsuccess event handler). You could even trim that down to:
$.post("json.php", {json : JSON.stringify(jsonObject)});
Then (in PHP) do:
<?php
$json = $_POST['json'];
if (json_decode($json) != null) { /* sanity check */
$file = fopen('new_map_data.json','w+');
fwrite($file, $json);
fclose($file);
} else {
// handle error
}
?>
这篇关于写JSON对象.json服务器上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!