JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
The "JSON" Object Type - parse() and stringify()
This section provides a quick description and a tutorial example script on the 'JSON' built-in object type, which is used to parse JSON strings to create JavaScript structured values and convert JavaScript structured values to JSON strings.
The JSON object type that offers 2 static functions to parse and generate JSON text strings:
1. JSON.parse() - Parses a text string from a JSON text string, constructs the JavaScript value. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
2. JSON.stringify() - Converts a JavaScript value to a JSON text string. An optional replacer function is specified to replace some values.
Here is a JavaScript code example, JSON.html, that shows you how to use the built-in JSON object:
<html> <body> <script type="text/javascript"> var object = {"msg":"Hello World!"}; var str = JSON.stringify(object); document.write("<p>JSON text string from JSON.stringify():</p>"); document.write("<pre>str = "+str+"</pre>"); var val = JSON.parse(str); document.write("<p>Value from JSON.parse():</p>"); document.write("<pre>val.msg = "+val.msg+"</pre>"); </script> </body> </html>
Open this example, JSON.html, in a Web browser. You see the following:
JSON text string from JSON.stringify(): str = {"msg":"Hello World!"} Value from JSON.parse(): val.msg = Hello World!
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
Defining Your Own Object Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
Using Functions as "Function" Objects
►Introduction to Built-in Object Types
Overview of Built-in Object Types
The "Object" Object Type - The Root Object Type
The "Global" Object Type - The Invisible Global Container
Global Properties and Functions Defined in ECMAScript
Global Properties and Functions Provided by "jrunscript"
The "Function" Object Type - Functions Are Objects
The "Array" Object Type - Arrays Are Objects
►The "JSON" Object Type - parse() and stringify()
The "String" Object Type - Not Equal to String Primitive Type
The "Boolean" Object Type - Wrapping Boolean Values into Objects
The "Number" Object Type - Not Equal to Number Primitive Type
The "Date" Object Type - Managing Dates and Times
The "RegExp" Object Type - Regular Expression Patterns
The "Error" Object Type - Runtime Exceptions
The "Math" Object Type - The Math Container
W3C's Document Object Model (DOM) Specifications