Convert JSON object to string

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will discuss
1. How to convert JSON object to string
2. How to convert string to JSON object

This is continuation Part 18. Please watch Part 18 from jQuery tutorial before proceeding.

Replace < with LESSTHAN symbol and > with GREATERTHAN symbol

The following example converts JSON array to a string. JSON.stringify() method converts a JSON object (or array) into a string.
<html>
<head>
<title></title>
<script type="text/javascript">

$(document).ready(function () {
var employeesJSON = [
{
"firstName": "Todd",
"lastName": "Grover",
"gender": "Male",
"salary": 50000
},
{
"firstName": "Sara",
"lastName": "Baker",
"gender": "Female",
"salary": 40000
}
];

var JSONString = JSON.stringify(employeesJSON);
$('#resultDiv').html(JSONString);
});
</script>
</head>
<body style="font-family:Arial">
<div id="resultDiv"></div>
</body>
</html>

Output :
[{"firstName":"Todd","lastName":"Grover","gender":"Male","salary":50000},
{"firstName":"Sara","lastName":"Baker","gender":"Female","salary":40000}]

The following example converts a string to a JSON array. JSON.parse() method converts a JSON string to JSON array. We then use the jQuery each() method to loop thru each employee JSON object and retrieve the respective property values.

<html>
<head>
<title></title>
<script type="text/javascript">

$(document).ready(function () {

var JSONString = '[{ "firstName": "Todd", "lastName": "Grover", "gender": "Male", "salary": 50000 }, { "firstName": "Sara", "lastName": "Baker", "gender": "Female", "salary": 40000 }]';

var employeesJSON = JSON.parse(JSONString);

var result = '';

$.each(employeesJSON, function (i, item) {
});

$('#resultDiv').html(result);
});
</script>
</head>
<body style="font-family:Arial">
<div id="resultDiv"></div>
</body>
</html>
Рекомендации по теме
Комментарии
Автор

Thank you so much sir for sharing these tutorials. Was stuck in filling an HTML table from json. Fortunately saw this tutorial and got the breakthrough. Thanks a lot.

vivekvinita
Автор

thanks for video, could you tell me what is the difference between js object and json object

tarekkhatib
Автор

The tutorial is useful Sir. I have a task to handle now. I want 2 dB one from MySql(with PHP) and other from MS SQL(with ASP.net). I want these two to synchronise. i.e. when I insert a record in MySql, , , it should automatically reflect in MS SQL and vice versa. It's like handling 2 applications. Will the use of JSON help to achieve my task. Or is there any other API or some other wway to do it. Please Guide me through. Thank youy.

jenarthanMohan
Автор

Very nice collection of videos sir. Thank you so much. Although I would like to clarify one thing: At 6:40 you say, Google Chrome doesn't show error in a better way. Well, in developer tool, under 'console', you can see the errors in a better way.

rahuldwivedi
Автор

pls share the url for complete playlist of JSON tuorial..

prabhapower
Автор

how are we expected to remember so much.
This guy seems to remember everything

malharjajoo
Автор

pls clarify how the call back function is working

ManiMaran-wnsc
Автор

It's important to note that there is a difference between the $().html( ) and $().text() methods

malharjajoo
Автор

JSON.parse() getting an error while getting the data from php page and recievong through ajax

abhitutorials
Автор

why we passing in a function item parameter

ManiMaran-wnsc
Автор

What does i mean in the function? 
It is not used any place in the function though. I'm very new learner in Jquery and Javascript. If my question is meaningless, I'm sorry. I just want to know what is the purpose of argument i in the function?

jimme
Автор

JSON objects are not defined using any variable, they contains only property name with in inverted commas. But you are defining it using a variable? I am confused, kindly help.

remoteXJunkie