473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 12250
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

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

Similar topics

2
by: bylabylamo | last post by:
i have a problem converting a json string that i get from a servlet into a json object using eval function of java script. any one please help me..........
1
by: valheru | last post by:
Hi, I'm not sure if this question even makes sense, so bear with me here. I'm using a tool called HtmlUnit (which is like JUnit) to send a http request to my web application. The http address...
1
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a asp.net app, in client I create a Complicate json string, for example: { a: ‘a’, b : { b ; ‘b’ } } If .net has corresponding type, it could be deserialized, for example: ...
5
by: Nick S | last post by:
Hi Is there anyway to iterate through a json object and retrieve the name of the property as well as the value. For exampe. Imagine this object { title: '1', bedrooms: '2',
3
Kelicula
by: Kelicula | last post by:
Hi all, I am usually a Perl programmer, I have some background in javascript and am attempting to create a Googleish selector div. Please bear with me, excuse the long introduction... Here's...
0
by: JonTwend | last post by:
I am working with the Twitter API and finding it great fun and generally easy to use. But I am having trouble getting to the data in the trends/current.json file. I am able to loop through the...
1
by: Alex Andrew | last post by:
Hai I am sending a JSON object from a servlet to JSP using AJAX. My JSON object contains a String value inside. and that string contains double quotes within that. My JSON does not parse it. I get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.