Home > DeveloperSection > Articles > Parsing a Message in JavaScript using JSON

Parsing a Message in JavaScript using JSON


JSON JSON 
Ratings:
0 Comment(s)
 5292  View(s)
Rate this:

Parsing a Message in JavaScript using JSON

This article is going to explain on how to parse a message in JavaScript using JSON. We can parse the message with JSON in JavaScript by using the method String.parseJSON(filter).  It parses the JSON message to a string or object. The parameter filter is optional in this method which is used to filter message or transform their results. This method internally uses the JavaScript's method eval() to parse messages.

To understand the Message Parsing in JavaScript using JSON writes down the following line of code.

<html>

<head>

<title>

JSON Message Parsing With Example

</title>

<script language="javascript" src="json2.js"></script>

<script language="javascript" >

                var StudentsDetails = {

            ////Decalration of array to collect record of Student.

                   // Information of MCA students

                        "MCAStudent": [{

                         "StudentName": "Kumar Vishu",

                         "Roll_No":100,

                         "Stream": "Computer Science",

                         "Course": "MCA"

                    },

                       {

                         "StudentName": "Tarun Bindra",

                         "Roll_No":101,

                         "Stream": "Science",

                         "Course": "B.Tech"

                    }

            ]

               

                }

                                // Printing all array values

                    var i=0

                    var dataCollection = new Array();

                    for(i=0;i<StudentsDetails.MCAStudent.length;i++)

                    {         

                        // Push the data into array

                            dataCollection.push(StudentsDetails.MCAStudent[i].StudentName);

                            dataCollection.push(StudentsDetails.MCAStudent[i].Roll_No);

                            dataCollection.push(StudentsDetails.MCAStudent[i].Stream);

                            dataCollection.push(StudentsDetails.MCAStudent[i].Course);

                    }                                         

                         

                           alert("Welcome to JSON Message Example ");

                           // Use toJSONString() function for message parsing

                           alert(dataCollection.toJSONString());

</script>

</head>

<body>

Message parsing successfully in JavaScript using JSON.

</body>

</html>

Now save the above code in html format (Here I’m saving this file with ‘JSONParsing.html’ ) and execute with web browser.

Parsing a Message in JavaScript using JSON

Now click on button ‘Ok’ after that parsed message will be display in alert message box.

Parsing a Message in JavaScript using JSON

Highlighted area; showing the parse message in alert message box. Now click on button ‘Ok’.

Parsing a Message in JavaScript using JSON

This is the complete description on Parsing a Message in JavaScript using JSON.


Don't want to miss updates? Please click the below button!

Follow MindStick