Thursday, 11 September 2014

Learning how to make JSON....

So the key piece of code is JSON.stringify

This example shows it very simply:

var a = [];
a.push({ name: 'x1' });
a.push({ name: 'x2' });
console.log(JSON.stringify(a));

In the console log you get:

[{"name":"x1"},{"name":"x2"}] 


So testing this:
      var start = 21;
      var a = [];
      a.push({ name: start });
      a.push({ name: 'x2' });
      console.log(JSON.stringify(a));   

In the console log you get:
     [{"name":21},{"name":"x2"}] 

GOOD!!!!

VERY STRANGELY, EVEN WHEN I SEND IT AS JSON, THE SCRIPT I HAVE WRITTEN DOESN'T SEEM TO LIKE IT. 
I DON'T KNOW QUITE WHY!


Surprisingly, a variation on push seems to work:

           var start = 21;
           var end = 193;
           var color = "blue";
           
           var a =[];
           a.push({ 'start': start, 'end': end, 'color': color });

This gives data that the molecule renderer seems to like. 


No comments:

Post a Comment