I have been able to construct my jsonObject which is a collection of 3 sets of data 1) a polygon with variable number of vertices, 2) a variable list of feature points lat/lng 3) a corresponding list of feature attributes describing the feature points.
I want to send this information via ajax to my web server for processing.
From JSON.orgI have downloaded both json.js and json2.js, I see the relatively new json2 supersedes json. Also in json2 "toJSONstring() is no longer used. Instead it is " JSON.stringify(value, replacer, space)"
Below is my code and below that some sample data. My question is how do I execute line 34 of this code.
I have an object myJrec created on line 24. This object has to be json stringified? How do I do that? Note - I have linked the json2.js to my html page, so it is available to act upon myJrec.
Expand|Select|Wrap|Line Numbers
- //save layout section
- function saveLayout() {
- // Since this Polygon only has one path, we can call getPath()
- // to return the MVCArray of LatLngs
- var vertices = poly.getPath();
- var jobSite = '{"JOBSITE":[';
- // get job site location.
- for (var i =0; i < vertices.length; i++) {
- var xy = vertices.getAt(i);
- jobSite += '{"VERTICE":"' + xy.lat() +" " + xy.lng() + '"},';
- }
- jobSite = jobSite.substring(0,jobSite.lastIndexOf(","))+']';
- //get marked features on job site
- var featureAttrib = '"FEATUREATTRIB":[';
- var featurePoints = '"FEATUREPOINTS":[';
- for (var i = 0; i < markersArray.length; i++ )
- {
- featureAttrib += '{"FEATURE":"' + exchangeArray[i].feature + '","ICON":"' + exchangeArray[i].iconColor + '"},';
- featurePoints += '{"POINT":"' + markersArray[0].position.lat() +' '+ markersArray[0].position.lng() + '"},';
- }
- featureAttrib = featureAttrib.substring(0,featureAttrib.lastIndexOf(","))+']';
- featurePoints = featurePoints.substring(0,featurePoints.lastIndexOf(","))+']}';
- var myJrec = eval("("+jobSite + ',' + featureAttrib + ','+ featurePoints+")");
- var request = window.ActiveXObject ?
- new ActiveXObject('Microsoft.XMLHTTP') :
- new XMLHttpRequest;
- var url = 'saveProjectLayout.exe?timeStamp=' + new Date().getTime();
- request.open("POST", url, true);
- request.onreadystatechange = updatePage;
- request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- request.send(myJrec.toJsonString());
- }
{"JOBSITE":[{"VERTICE":"41.75076232883921 -88.15228931877138"},{"VERTICE":"41.748264959811564 -88.15768592330934"},{"VERTICE":"41.747984800174116 -88.15771810981752"},{"VERTICE":"41.7480568413405 -88.15235369178774"}],"FEATUREATTRIB":[{"FEATURE":"p1","ICON":"iconb"},{"FEATURE":"p2","I CON":"icong"},{"FEATURE":"p3","ICON":"iconr"},{"FE ATURE":"p4","ICON":"iconr"},{"FEATURE":"p5","ICON" :"icony"}],"FEATUREPOINTS":[{"POINT":"41.74914945578666 -88.1549768922043"},{"POINT":"41.74914945578666 -88.1549768922043"},{"POINT":"41.74914945578666 -88.1549768922043"},{"POINT":"41.74914945578666 -88.1549768922043"},{"POINT":"41.74914945578666 -88.1549768922043"}]}