Basic help with JSON

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Basic help with JSON

    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
    Code:
        var featureAttrib = []
        for (var i = 0; i < fArray.length; i++ )
        {
            featureAttrib += {"FEATURE": fArray[i].feature, "ICON": fArray[i].iconColor };
        }
    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.
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    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.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var fArray = new Array("Point1","Point2","Point3","Point4","Point5");
    var pArray = new Array("red1","red2","red3","red4","red5");
    var fAttrib =[];
    for(var i=0;i<fArray.length;i++)
       fAttrib += {"FEATURE":fArray[i],"ICON":pArray[i]};
    
    var fAttrib1 ='{"myJSON":[';
    
    for(var i=0;i<fArray.length;i++)
    {
      if(i==(fArray.length-1))
         fAttrib1 += '{"FEATURE":"'+fArray[i]+'","ICON":"'+pArray[i]+'"}';
      else
         fAttrib1 += '{"FEATURE":"'+fArray[i]+'","ICON":"'+pArray[i]+'"},';
    }
    
    fAttrib1 +=']}'  
    
    alert("Your Code "+fAttrib);
    
    alert("JSON String = "+fAttrib1);
    
    var fAttObj = eval("("+fAttrib1+")");
    
    alert(fAttObj);
    </script>
    <body>
    </body>
    </html>
    In the second alert you will get this
    {"myJSON":
    [
    {"FEATURE":"Poi nt1","ICON":"re d1"},
    {"FEATURE":"Poi nt2","ICON":"re d2"},
    {"FEATURE":"Poi nt3","ICON":"re d3"},
    {"FEATURE":"Poi nt4","ICON":"re d4"},
    {"FEATURE":"Poi nt5","ICON":"re d5"}
    ]
    }

    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      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
      Code:
      function saveLayout() {
      
      	var vertices = poly.getPath();
      	var jobSite = '{"JOBSITE":[';
      	// get job site location.
      	for (var i =0; i < vertices.length; i++) {
      		var xy = vertices.getAt(i);
      			jobSite += '{"VERTICE":"' + xy.lat() +" " + xy.lng() + '"},';
      	}
      	jobSite = jobSite.substring(0,jobSite.lastIndexOf(","))+']';
      	//get marked features on job site
      	var featureAttrib = '"FEATUREATTRIB":[';
      	var featurePoints = '"FEATUREPOINTS":[';
      	for (var i = 0; i < markersArray.length; i++ )
      	{
      			featureAttrib += '{"FEATURE":"' + exchangeArray[i].feature + '","ICON":"' + exchangeArray[i].iconColor + '"},';
      			featurePoints += '{"POINT":"'   + markersArray[0].position.lat() +' '+ markersArray[0].position.lng() + '"},';
      	}
      	featureAttrib = featureAttrib.substring(0,featureAttrib.lastIndexOf(","))+']';
      	featurePoints = featurePoints.substring(0,featurePoints.lastIndexOf(","))+']}';
      	var myJrec  = eval("("+jobSite + ',' + featureAttrib + ','+ featurePoints+")");
      
      	var request = window.ActiveXObject ?
      		new ActiveXObject('Microsoft.XMLHTTP') :
      	    new XMLHttpRequest;
      
      	var url = '/SXYZ/projectLayout2.exe?timeStamp=' + new Date().getTime();
      	request.open("POST", url, true);
      	request.onreadystatechange = updatePage;
      	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      	request.send(myJrec.toJsonString());
      
      }

      Comment

      • RamananKalirajan
        Contributor
        • Mar 2008
        • 608

        #4
        Keep Moving Forward dude.. I am happy you got the solution.

        Thanks and Regards
        Ramanan Kalirajan

        Comment

        Working...