Solved

Creating a JSON object during run time?

Posted on 2006-11-07
6
3,561 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now