SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Javascript Tutorials and Javascript Frameworks jQuery resources, tools, articles, code samples and downloads for Web Programmers


Convert Javascript Object to String using JSON.Stringify Method

Using Javascript JSON.stringify( object ) method, web developer can convert an object to string easily in their web project developments. In this Javascript tutorial, I will demonstrate to convert a Javascript object to string using JSON.Stringify method. As a second example, I will show how easily programmers can convert an array of objects to string using the same JSON.Stringify Javascript method.

What is JSON.Stringify Method used for?

I assume web developers already know that JSON stands for Javascript Object Notation. Web programmers can use the JSON.Stringify method to convert object to string in Javascript. Let's make some JSON.Stringify examples.


JSON.Stringify to Convert Object to String in Javascript

First I'll create an object which will demonstrate a ball to use move it on an HTML5 canvas object. Simple Javascript object named ball will have a few attributes or properties as seen in below code blocks.

<script>
 function Ball(id, diameter, speed, angle, xPos, yPos) {
  this.id = id;
  this.diameter = diameter;
  this.speed = speed;
  this.angle = angle;
  this.xPos = xPos;
  this.yPos = yPos;
 }

 var newBall = new Ball(1, 10, 25, 130, 200, 300);
 document.write(JSON.stringify(newBall));
</script>
Code

And here is the output:

Javascript developers can use JSON.stringify method and test with on their own custom developments to convert object to string.


Convert an Array to String in Javascript using JSON.Stringify

In this part, developers will see how an array of objects is converted to string using JSON.Stringify Javascript command.

Let's create an array of objects, I mean balls that we have defined in previous step.

var balls = new Array();
Code

Then as we did in previous step, define the ball object type in Javascript. As following step we can add three balls to the "balls" array of objects as seen in below Javascript code

var balls = new Array();

function Ball(id, diameter, speed, angle, xPos, yPos) {
 this.id = id;
 this.diameter = diameter;
 this.speed = speed;
 this.angle = angle;
 this.xPos = xPos;
 this.yPos = yPos;
}

var newBall = new Ball(1, 10, 25, 130, 200, 300);
balls.push(newBall);

var anotherBall = new Ball(2, 15, 30, 90, 400, 100);
balls.push(anotherBall);

var lastBall = new Ball(3, 12, 20, 45, 100, 250);
balls.push(lastBall);

// convert objects array to string using Javascript JSON.Stringify
document.write(JSON.stringify(balls));
Code

Here is the output of above sample Javascript code:

I hope this Javascript tutorial demonstrating how to convert objects to string expressions is useful for web programmers.



Javascript


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.