[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3573
  • Last Modified:

Creating a JSON object during run time?

I need to create a JSON object at run time.  I guess this would be an array of name/value pairs.  I am a newbie, so I'm not sure this is stated correctly.

If statically created, it would look like:

var json = [
   { x: 0, y: 110 },
   { x: 10, y: 24 },
   { x: 15, y: 63 },
   { x: 25, y: 5 },
   { x: 40, y: 98 },
   { x: 45, y: 54 },
   { x: 120, y: 1 }
 ];

So, the question is, how do I create this during run time?

Thanks,
-
Robert

0
rubear
Asked:
rubear
  • 3
  • 3
1 Solution
 
inubisCommented:
rubear,

You can use the very same notation. E.g. to put two vars from a form into a json formated object:

var a = docuemnt.getElementById('a').value;
var b = docuemnt.getElementById('b').value;
var c = docuemnt.getElementById('a').value;
var d = docuemnt.getElementById('b').value;

var json = [
  {"x": a, "y": b},
  {"x": c, "y": d},
];

Sam
0
 
inubisCommented:
Also, as a side note, the JSON specification requires that member names are strings where as JavaScript is more relaxed. Your example was perfectly valid javascript but in JSON it should have been:

var json = [
   { "x": 0, "y": 110 },
   { "x": 10, "y": 24 },
   { "x": 15, "y": 63 },
   { "x": 25, "y": 5 },
   { "x": 40, "y": 98 },
   { "x": 45, "y": 54 },
   { "x": 120, "y": 1 }
 ];

See http://json.org/ for more.
0
 
rubearAuthor Commented:
Thanks for the tip about member names being strings.  What I really need to do is create the JSON object from a large data array.  The size could be perhaps 2,000 members.  So the above method won't work.  For example creating teh JSON object from XML data.

Thanks,
-
Robert
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
inubisCommented:
Ah, okay. What do you want to use the data for?

JSON is just javascript objects, arrays, strings and numbers.

var myBigArray = getBigArray(); // Get a big 2d array from where ever
var myBigJson = new Array();

for(var i=0; i<myBigArray ; i++)
{
  myBigJson[i] = new Object();
  myBigJson[i]["x"] = myBigArray[i][0];
  myBigJson[i]["y"] = myBigArray[i][1];
  // Or Just
  myBigJson[i] = {"x":myBigArray[i][0], "y":myBigArray[i][1]};
}

What do you want to do with the JSON data after you create it?
If you want to use it on javascript then the above method is fine.
If you wanted to pass it back to the server or save it in a text file or something then you would need a function that would covert it to text.

-Sam
0
 
rubearAuthor Commented:
I am trying to plot via an dojo SVG library which is wanting an object in this format.  I finally tried this, which works:

json = [];
for(i=0; i !=360; i++)
{
    json.push( {"x":i, "y": Math.sin(i*Math.PI/180)} );
}

I'll try your method above as well.

Thanks,
-
Robert
0
 
rubearAuthor Commented:
OK, thanks,
Both methods work.  

The more proper syntax for my mthod is probably:

 var json = new Array();

for(var i=0; i !=360; i++)
{
     json.push( {"x":i, "y": 100+ 50*Math.sin(i*Math.PI/180)} );
}

I'll accept your awnser, thanks!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now