469,337 Members | 6,005 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Basic help with JSON

Claus Mygind
571 512MB
what am I doing wrong in creating the json object here

I have a javaScript array the data looks like this
0
feature "Point 1"
pinColor "red"

1
feature "Point 2"
pinColor "blue"

2
feature "Point 3"
pinColor "green"

3
feature "Point 4"
pinColor "yellow"

I use the following code to try to create the json object
Expand|Select|Wrap|Line Numbers
  1.     var featureAttrib = []
  2.     for (var i = 0; i < fArray.length; i++ )
  3.     {
  4.         featureAttrib += {"FEATURE": fArray[i].feature, "ICON": fArray[i].iconColor };
  5.     }
  6.  
What I get is
[object Object][object Object][object Object]

I know that what I want to get is
{"feature":"p1", "icon":"red"},{"feature":"p2", "icon":"blue"},...

Once I get past this part I can ask my next basic question.
Sep 16 '10 #1

✓ answered by RamananKalirajan

Hi Claus Mygind,
Your question is pretty easy. You have done a mistake in framing the object. Here with I am giving a sample of how to create a Object of your desired format.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var fArray = new Array("Point1","Point2","Point3","Point4","Point5");
  5. var pArray = new Array("red1","red2","red3","red4","red5");
  6. var fAttrib =[];
  7. for(var i=0;i<fArray.length;i++)
  8.    fAttrib += {"FEATURE":fArray[i],"ICON":pArray[i]};
  9.  
  10. var fAttrib1 ='{"myJSON":[';
  11.  
  12. for(var i=0;i<fArray.length;i++)
  13. {
  14.   if(i==(fArray.length-1))
  15.      fAttrib1 += '{"FEATURE":"'+fArray[i]+'","ICON":"'+pArray[i]+'"}';
  16.   else
  17.      fAttrib1 += '{"FEATURE":"'+fArray[i]+'","ICON":"'+pArray[i]+'"},';
  18. }
  19.  
  20. fAttrib1 +=']}'  
  21.  
  22. alert("Your Code "+fAttrib);
  23.  
  24. alert("JSON String = "+fAttrib1);
  25.  
  26. var fAttObj = eval("("+fAttrib1+")");
  27.  
  28. alert(fAttObj);
  29. </script>
  30. <body>
  31. </body>
  32. </html>
In the second alert you will get this
{"myJSON":
[
{"FEATURE":"Point1","ICON":"red1"},
{"FEATURE":"Point2","ICON":"red2"},
{"FEATURE":"Point3","ICON":"red3"},
{"FEATURE":"Point4","ICON":"red4"},
{"FEATURE":"Point5","ICON":"red5"}
]
}

Thanks and Regards
Ramanan Kalirajan

3 1120
RamananKalirajan
607 512MB
Hi Claus Mygind,
Your question is pretty easy. You have done a mistake in framing the object. Here with I am giving a sample of how to create a Object of your desired format.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var fArray = new Array("Point1","Point2","Point3","Point4","Point5");
  5. var pArray = new Array("red1","red2","red3","red4","red5");
  6. var fAttrib =[];
  7. for(var i=0;i<fArray.length;i++)
  8.    fAttrib += {"FEATURE":fArray[i],"ICON":pArray[i]};
  9.  
  10. var fAttrib1 ='{"myJSON":[';
  11.  
  12. for(var i=0;i<fArray.length;i++)
  13. {
  14.   if(i==(fArray.length-1))
  15.      fAttrib1 += '{"FEATURE":"'+fArray[i]+'","ICON":"'+pArray[i]+'"}';
  16.   else
  17.      fAttrib1 += '{"FEATURE":"'+fArray[i]+'","ICON":"'+pArray[i]+'"},';
  18. }
  19.  
  20. fAttrib1 +=']}'  
  21.  
  22. alert("Your Code "+fAttrib);
  23.  
  24. alert("JSON String = "+fAttrib1);
  25.  
  26. var fAttObj = eval("("+fAttrib1+")");
  27.  
  28. alert(fAttObj);
  29. </script>
  30. <body>
  31. </body>
  32. </html>
In the second alert you will get this
{"myJSON":
[
{"FEATURE":"Point1","ICON":"red1"},
{"FEATURE":"Point2","ICON":"red2"},
{"FEATURE":"Point3","ICON":"red3"},
{"FEATURE":"Point4","ICON":"red4"},
{"FEATURE":"Point5","ICON":"red5"}
]
}

Thanks and Regards
Ramanan Kalirajan
Sep 17 '10 #2
Claus Mygind
571 512MB
Thanks you for your simple code example.

Your instructions have been extremely helpful. Now I can go on to ask my next question. I will start a new thread for that.

My final code looks like this
Expand|Select|Wrap|Line Numbers
  1. function saveLayout() {
  2.  
  3.     var vertices = poly.getPath();
  4.     var jobSite = '{"JOBSITE":[';
  5.     // get job site location.
  6.     for (var i =0; i < vertices.length; i++) {
  7.         var xy = vertices.getAt(i);
  8.             jobSite += '{"VERTICE":"' + xy.lat() +" " + xy.lng() + '"},';
  9.     }
  10.     jobSite = jobSite.substring(0,jobSite.lastIndexOf(","))+']';
  11.     //get marked features on job site
  12.     var featureAttrib = '"FEATUREATTRIB":[';
  13.     var featurePoints = '"FEATUREPOINTS":[';
  14.     for (var i = 0; i < markersArray.length; i++ )
  15.     {
  16.             featureAttrib += '{"FEATURE":"' + exchangeArray[i].feature + '","ICON":"' + exchangeArray[i].iconColor + '"},';
  17.             featurePoints += '{"POINT":"'   + markersArray[0].position.lat() +' '+ markersArray[0].position.lng() + '"},';
  18.     }
  19.     featureAttrib = featureAttrib.substring(0,featureAttrib.lastIndexOf(","))+']';
  20.     featurePoints = featurePoints.substring(0,featurePoints.lastIndexOf(","))+']}';
  21.     var myJrec  = eval("("+jobSite + ',' + featureAttrib + ','+ featurePoints+")");
  22.  
  23.     var request = window.ActiveXObject ?
  24.         new ActiveXObject('Microsoft.XMLHTTP') :
  25.         new XMLHttpRequest;
  26.  
  27.     var url = '/SXYZ/projectLayout2.exe?timeStamp=' + new Date().getTime();
  28.     request.open("POST", url, true);
  29.     request.onreadystatechange = updatePage;
  30.     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  31.     request.send(myJrec.toJsonString());
  32.  
  33. }
  34.  
  35.  
  36.  
Sep 20 '10 #3
RamananKalirajan
607 512MB
Keep Moving Forward dude.. I am happy you got the solution.

Thanks and Regards
Ramanan Kalirajan
Sep 20 '10 #4

Post your reply

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

Similar topics

3 posts views Thread by astarocean | last post: by
2 posts views Thread by Ralph | last post: by
3 posts views Thread by Adam | last post: by
5 posts views Thread by Otto Wyss | last post: by
1 post views Thread by Andrew Poulos | last post: by
11 posts views Thread by kj | last post: by
1 post views Thread by SirCodesALot | last post: by
3 posts views Thread by you | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.