Saturday 1 September 2018

JavaScript: Convert javascript object to json string

JSON.stringify(object) method converts JavaScript object to JSON string.

stringify.html
<!DOCTYPE html>
<html>

<head>
    <title>Convert Java object to string</title>
</head>

<body>
    <h1>String format of employees object is</h1>
    <div id="data">

    </div>

    <script type="text/javascript">
        var employees = [{
            "firstName": "Hari Krishna",
            "lastName": "Gurram",
            "age": 27
        }, {
            "firstName": "Kiran Kumar",
            "lastName": "Darsi",
            "age": 28
        }];

        var str = JSON.stringify(employees);
        document.getElementById("data").innerHTML = str;
    </script>
</body>

</html>

Open above page in browser, you can able to see following text.

String format of employees object is

[{"firstName":"Hari Krishna","lastName":"Gurram","age":27},{"firstName":"Kiran Kumar","lastName":"Darsi","age":28}]




Previous                                                 Next                                                 Home

No comments:

Post a Comment