469,277 Members | 2,241 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

To implement json do you load something first

Claus Mygind
571 512MB
In the following example I stop at ".toJSONString()"

I have read all the articles on formatting JSON objects and arrays and I understand how it is a subset of JavaScript. But I have not been able to find a small (complete html page with headers) example. only small snippets of code, so it is hard to get the whole picture.

Can someone point me to a complete example

Expand|Select|Wrap|Line Numbers
  1.     var request = window.ActiveXObject ?
  2.         new ActiveXObject('Microsoft.XMLHTTP') :
  3.         new XMLHttpRequest;
  4.  
  5.     var url = '/SXYZ/projectLayout2.exe?timeStamp=' + new Date().getTime();
  6.     request.open("POST", url, true);
  7.     request.onreadystatechange = updatePage;
  8.     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  9.     request.send(myJrec.toJSONString());
  10.  
Sep 13 '10 #1

✓ answered by Claus Mygind

Ok I think I got it.
1) From http://www.json.org/js.html
2) Under javaScript click on the json2.js
3) Go to bottom of page and click on "The open source code of a JSON parser and JSON stringifier is available."
4) Save the displayed content to your computer.
5) Minify the file
6) and link it to your app.

Then all the code snippet examples will work.

3 1503
gits
5,390 Expert Mod 4TB
here is nearly everything noteworthy about JSON - http://json.org/ - what is your exact problem? you have JavaScript arrays/objects and want to write a function that creates a valid JSON-string from it?
Sep 14 '10 #2
Claus Mygind
571 512MB
Thank you for your reply. The reference you sent to json.org is where I started and I also read this material https://www.ibm.com/developerworks/w...a-ajaxintro10/

However both lack a complete example of how to set up json. I understand the basic concept but there must be a library that gets installed. At first my impression was that modern browsers included json much like they include javaScript but that does not appear to be the case.

You ask what am I attempting to do.

I am looking for the best method to send data to the web server with the least amount of overhead. json seemed to be the best alternative.

Here I what I want to send. Basically there are only 3 pieces of information, however each piece contains variable amounts of data.

The user's computer (client) consists of a Google map and a data panel. The user will outline a job site and identify features on the job site with push pins. There will be a polygon (the job site) with variable number of sides, point coordinates and an identifier for each feature (push pin) placed on the job site.

POLYGON((
41.7506422652437 -88.15240733596804,
41.7482409461762 -88.15781466934206,
41.74801681848022 -88.15245025131227))

array of feature point
pinArray
0
position "41.74843305500714, -88.1563984629822"

1
position "41.74849709115636, -88.15476767990114"

2
position "41.74955367839665, -88.1538235423279"

3
position "41.74852910920703, -88.15369479629518"

array of feature objects descriptions
0
feature "Point 1"
pinColor "red"

1
feature "Point 2"
pinColor "blue"

2
feature "Point 3"
pinColor "green"

3
feature "Point 4"
pinColor "yellow"

Here is my code so far
Expand|Select|Wrap|Line Numbers
  1. //save layout section
  2. function saveLayout() {
  3.  
  4.   /*
  5.     vertices contains more info than needed so
  6.     only transmit coordinates in a format that
  7.     mySQL can store in a polygon field
  8.   */
  9.   var vertices = poly.getPath();
  10.   var jobSite = "POLYGON((";
  11.  
  12.   // get job site location.
  13.   for (var i =0; i < vertices.length; i++) {
  14.      var xy = vertices.getAt(i);
  15.      jobSite += xy.lat() +" " + xy.lng() +",";
  16.   }
  17.   jobSite = jobSite.substring(0,jobSite.lastIndexOf(","))+"))";
  18.  
  19.   //get marked features on job site
  20.   var featureAttrib = "";
  21.   var featurePoints = "";
  22.   for (var i = 0; i < markersArray.length; i++ )
  23.   {
  24.     featurePoints += markersArray[i].position+",";
  25.         featureAttrib += {"FEATURE": exchangeArray[i].feature, "ICON": exchangeArray[i].iconColor };
  26.   }
  27.  
  28.   featureAttrib = featureAttrib.substring(0,featureAttrib.lastIndexOf(","));
  29.  
  30.   featurePoints = featurePoints.substring(0,featurePoints.lastIndexOf(","));
  31.  
  32.   var myJrec  = {"JOBSITE" : jobSite};
  33.   myJrec += {"FEATUREATTRIB": [featureAttrib] };
  34.   myJrec += {"FEATUREPOINTS" : featurePoints};
  35.  
  36.   var request = window.ActiveXObject ?
  37.         new ActiveXObject('Microsoft.XMLHTTP') :
  38.         new XMLHttpRequest;
  39.  
  40.   var url = '/SXYZ/projectLayout2.exe?timeStamp=' + new Date().getTime();
  41.     request.open("POST", url, true);
  42.     request.onreadystatechange = updatePage;
  43.     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  44.     request.send(myJrec.toJSONString());
  45.  
  46. }
  47.  
  48. function updatePage()
  49. {
  50.     alert("complete");
  51. }
  52.  
  53.  
Please disregard other errors in my coding for now. I am only interested in how I might be able to send via "Post" and ajax request in a json format to the server.

It appears from what I have read, you can format the data into a json format style ie: arrays, then stringify the data again in a json style (see my original post on top), but that is not the case.

Again is there a small example that shows all the code necessary to successfully transmit json data including any header link to outside libraries?
Sep 16 '10 #3
Claus Mygind
571 512MB
Ok I think I got it.
1) From http://www.json.org/js.html
2) Under javaScript click on the json2.js
3) Go to bottom of page and click on "The open source code of a JSON parser and JSON stringifier is available."
4) Save the displayed content to your computer.
5) Minify the file
6) and link it to your app.

Then all the code snippet examples will work.
Sep 16 '10 #4

Post your reply

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

Similar topics

21 posts views Thread by Morten Aune Lyrstad | last post: by
20 posts views Thread by Luke Matuszewski | last post: by
reply views Thread by fredrik | last post: by
9 posts views Thread by SAL | last post: by
3 posts views Thread by =?Utf-8?B?WmVyb2dvcw==?= | last post: by
reply views Thread by David Jackson | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.