JSON object toJSONstring( )

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    JSON object toJSONstring( )

    I see a lot of references to "toJSONstring() ", but nothing on how to construct it or if it defined in some library.

    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 "toJSONstri ng() 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.

    Code:
    //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());
    
    }
    Sample data my json object myJrec

    {"JOBSITE":[{"VERTICE":"41. 75076232883921 -88.152289318771 38"},{"VERTICE" :"41.7482649598 11564 -88.157685923309 34"},{"VERTICE" :"41.7479848001 74116 -88.157718109817 52"},{"VERTICE" :"41.7480568413 405 -88.152353691787 74"}],"FEATUREATTRIB ":[{"FEATURE":"p1" ,"ICON":"iconb" },{"FEATURE":"p 2","ICON":"icon g"},{"FEATURE": "p3","ICON":"ic onr"},{"FEATURE ":"p4","ICON":" iconr"},{"FEATU RE":"p5","ICON" :"icony"}],"FEATUREPOINTS ":[{"POINT":"41.74 914945578666 -88.154976892204 3"},{"POINT":"4 1.7491494557866 6 -88.154976892204 3"},{"POINT":"4 1.7491494557866 6 -88.154976892204 3"},{"POINT":"4 1.7491494557866 6 -88.154976892204 3"},{"POINT":"4 1.7491494557866 6 -88.154976892204 3"}]}
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    I got this one figured out. Actually pretty easy

    from this
    request.send(my Jrec.toJsonStri ng());

    to this
    request.send(jS ON.stingify(myJ rec));

    with the the json2.js file linked to the html page

    Comment

    Working...