473,399 Members | 3,106 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,399 software developers and data experts.

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 1281
RamananKalirajan
608 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
608 512MB
Keep Moving Forward dude.. I am happy you got the solution.

Thanks and Regards
Ramanan Kalirajan
Sep 20 '10 #4

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

Similar topics

3
by: astarocean | last post by:
i'm using maildrophost to sendmail and wrote a script for it when the script is called directly from web request , a letter geneated with right things but when the script is called from...
2
by: Ralph | last post by:
Hi I have problem converting JSON string to an array. The best way to explain this is to show you some code. So : try { var response = that.AjaxReq.responseText.parseJSON(); } catch (myErr)...
3
by: Adam | last post by:
I'm trying to retrieve some values from a json object, but instead it's giving me the property name. For example: var json = { "glossary": { "title": "example glossary" } }; console.log(json);...
5
by: Otto Wyss | last post by:
I've now been looking for a week for a simple but useful sample on how to get a list of entries (persons) via an XMLHttpRequest using Json/PHP on the server. So far I've found about a thousend...
2
by: Newbie19 | last post by:
I was wondering what is the best concept or idea to mixing visual basic with ASP.net page that runs a SQL query on a database. The Visual Basic would be pulling data from another source that is not...
1
by: Andrew Poulos | last post by:
I'm building some e-learning that's managed by an learning management system (LMS). The LMS allows only one "field" for me to store custom lesson data. The e-learning currently uses a number of...
11
by: kj | last post by:
I would like to convert a form that currently uses the traditional CGI sequence (i.e. the action associated with the form sends a POST request to a server-side CGI script) to one that uses AJAX to...
1
by: SirCodesALot | last post by:
Hi All, Is it possible to create an JSON object from a string? For example var myArray = new Array(10) for (var i=0;i<10;i++) { myArray.push("{idx:"+i+",val:"+i+"}"); }
3
by: you | last post by:
Hi there, File manupulation is difficult in Perl. I'd like to modify a file with JSON format. For example, test.conf is: { "tests" : , ... (and so on) }
5
realin
by: realin | last post by:
hiya guys, Its been long i came here to discuss my problems. Hoep everything is same and i am gonna get quick reply :D Well i have certain static data which i need to display using...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.