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 jQuery and Javascript Tutorials and Code Examples
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.






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>

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();

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));

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

Javascript Articles

Javascript Forums












Javascript Tutorials, Articles and Javascript Codes

Morris Number Sequence Puzzle Solution in Javascript
Javascript Array Puzzle: People with Sword Around a Circle
Sort Array in Random Order using Javascript Shuffle Function
Javascript MultiDimensional Array example codes
How many times a day a clock's hands overlap?
Javascript For Loop Sample Code
Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space
Javascript String Trim with Javascript Trim Function Examples
Javascript String Split Function Example & Split Javascript Tutorial
Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
How to Enable / Disable Submit Button using Javascript According to any CheckBox is Checked on a WEB Page
Error Handling in JavaScript using Try...Catch Statement
How to display or unhide hidden table rows and hide or toggle a table row using Javascript functions
How to Replace All Occurrences of a String using Javascript Replace Function
How to autoformat and mask telephone numbers with Javascript
How to get key codes of characters using javascript code and key code list
Javascript Forums




Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems