Solved

Creating a JSON object during run time?

Posted on 2006-11-07
6
3,564 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

810 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