writing JSON object to .json file on server

AlCoHoLiC_08

New Member
I'm trying to write my JSON object to a .json file on the server. The way I'm doing this now is:JavaScript:\[code\]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 } });};\[/code\]PHP:\[code\]<?php $json = $_POST['json']; $info = json_encode($json); $file = fopen('new_map_data.json','w+'); fwrite($file, $info); fclose($file);?>\[/code\]It is writing fine and the information seems to be correct, but it is not rendering properly. It is coming out as:\[code\]{"metros":["{\\\"code\\\":\\\"SCL\\\",\\\"name\\\":\\\"Santiago\\\",\\\"country\\\":\\\"CL\\\",\\\"continent\\\":\\\"South America\\\",\\\"timezone\\\":-4,\\\"coordinates\\\":{\\\"S\\\":33,\\\"W\\\":71},\\\"population\\\":6000000,\\\"region\\\":1}",\[/code\]... but I'm expecting this:\[code\]"metros" : [ { "code" : "SCL" , "name" : "Santiago" , "country" : "CL" , "continent" : "South America" , "timezone" : -4 , "coordinates" : {"S" : 33, "W" : 71} , "population" : 6000000 , "region" : 1 } ,\[/code\]Any idea why I'm getting all of these slashes and why it is all on one line?Thanks,Hristo
 
Back
Top