469,323 Members | 1,517 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

JSON object toJSONstring( )

Claus Mygind
571 512MB
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 "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
  1. //save layout section
  2. function saveLayout() {
  3.  
  4.     // Since this Polygon only has one path, we can call getPath()
  5.     // to return the MVCArray of LatLngs
  6.     var vertices = poly.getPath();
  7.     var jobSite = '{"JOBSITE":[';
  8.     // get job site location.
  9.     for (var i =0; i < vertices.length; i++) {
  10.         var xy = vertices.getAt(i);
  11.             jobSite += '{"VERTICE":"' + xy.lat() +" " + xy.lng() + '"},';
  12.     }
  13.     jobSite = jobSite.substring(0,jobSite.lastIndexOf(","))+']';
  14.     //get marked features on job site
  15.     var featureAttrib = '"FEATUREATTRIB":[';
  16.     var featurePoints = '"FEATUREPOINTS":[';
  17.     for (var i = 0; i < markersArray.length; i++ )
  18.     {
  19.             featureAttrib += '{"FEATURE":"' + exchangeArray[i].feature + '","ICON":"' + exchangeArray[i].iconColor + '"},';
  20.             featurePoints += '{"POINT":"'   + markersArray[0].position.lat() +' '+ markersArray[0].position.lng() + '"},';
  21.     }
  22.     featureAttrib = featureAttrib.substring(0,featureAttrib.lastIndexOf(","))+']';
  23.     featurePoints = featurePoints.substring(0,featurePoints.lastIndexOf(","))+']}';
  24.     var myJrec  = eval("("+jobSite + ',' + featureAttrib + ','+ featurePoints+")");
  25.  
  26.     var request = window.ActiveXObject ?
  27.         new ActiveXObject('Microsoft.XMLHTTP') :
  28.         new XMLHttpRequest;
  29.  
  30.     var url = 'saveProjectLayout.exe?timeStamp=' + new Date().getTime();
  31.     request.open("POST", url, true);
  32.     request.onreadystatechange = updatePage;
  33.     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  34.     request.send(myJrec.toJsonString());
  35.  
  36. }
  37.  
  38.  
Sample data my json object myJrec

{"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"}]}
Sep 20 '10 #1

✓ answered by Claus Mygind

I got this one figured out. Actually pretty easy

from this
request.send(myJrec.toJsonString());

to this
request.send(jSON.stingify(myJrec));

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

1 11285
Claus Mygind
571 512MB
I got this one figured out. Actually pretty easy

from this
request.send(myJrec.toJsonString());

to this
request.send(jSON.stingify(myJrec));

with the the json2.js file linked to the html page
Sep 21 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post: by
5 posts views Thread by Nick S | last post: by
Kelicula
3 posts views Thread by Kelicula | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.