Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating a JSON object during run time?

Posted on 2006-11-07
6
Medium Priority
?
3,570 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Accepted Solution

by:
inubis earned 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

824 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