我正在尝试将我的
JSON对象写入服务器上的.json文件.我现在这样做的方式是:
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
}
});
};
$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
},
知道为什么我得到所有这些斜线以及为什么它都在一条线上?
谢谢,
斯托伊奇