Solved

Creating a JSON object during run time?

Posted on 2006-11-07
6
3,565 Views
Last Modified: 2008-03-17
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
Comment
Question by:rubear
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:inubis
ID: 17896586
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
 
LVL 3

Expert Comment

by:inubis
ID: 17896601
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
 

Author Comment

by:rubear
ID: 17923741
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Accepted Solution

by:
inubis earned 50 total points
ID: 17925478
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
 

Author Comment

by:rubear
ID: 17926005
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
 

Author Comment

by:rubear
ID: 17926047
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question