Saturday 25 February 2017

JSON Encoder and Decoder For JavaScript

What is JSON?

Answer : JSON(json stands for JavaScript Object Notation) is a most lightweight and very faster then XML computer data interchange(object,array,etc..) format.

The Following List Of Characteristics of JSON

1st : JSON is very easy to read mode and write mode.
2nd : It is a most lightweight with very faster more than XML text-based interchange format Data.
3rd : JSON is all language (Global)independent Data.
Json encoder and decoder tool used to convert or computes json encoded format data and decoded format data strings for the given text:

Following the List of Tools for JSON Encoder and Decoder :

JSON.stringify : Example

1st : JSON.stringify({}); // Result is : ‘{}’
2nd : JSON.stringify(true); // Result is : ‘true’
3rd : JSON.stringify(‘ng4free’); // Result is : ‘”ng4free”‘
4th : JSON.stringify([2, ‘false’, false]); // Result is : ‘[2,”false”,false]’
5th : JSON.stringify({ j: 50 }); //Result is : ‘{“j”:50}’
3th : JSON.stringify({ abc: 5, def: 6 }); //Result is: ‘{“abc”:5,”def”:6}’ or ‘{“def”:6,”abc”:5}’

Convert – an object to a string in javascript, used to JSON.stringify function in javascript:

1
var simple_json_text_demo = JSON.stringify(name_your_object_data, null, 2);

Convert – JSON string to object in javascript, used to JSON.parse function in javascript:

1
var name_your_object_data = JSON.parse(simple_json_text_demo);

jQuery with json – encode and decode

1
2
3
4
var simpleappmyObj = {script: "example", "demo": "simpleblogspotng4free"};
$.toJSON(simpleappmyObj);
 
// Result is = : {"script":"example","demo":"simpleblogspotng4free"}

Converting a JSON Text data to a JavaScript Object with Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html>
<head>
<title>
JSON Encoder and Decoder For JavaScript with Example
</title>
</head>
<body class="well">
 
<h2>Create Simple Object from data of JSON String For JavaScript with Example</h2>
 
<p id="simpledemo"></p>
 
<script>
var text_all = '{"userlistex":[' +
'{"userfirstName":"ng4free","userlastName":"king" },' +
'{"userfirstName":"ng4free.com","userlastName":"wordpress" },' +
'{"userfirstName":"ngking","userlastName":"patel" }]}';
 
obj = JSON.parse(text_all);
document.getElementById("simpledemo").innerHTML =
obj.userlistex[1].userfirstName + " " + obj.userlistex[1].userlastName;
</script>
 
</body>
</html>

JSON Format supports – DataTypes List

1 : Number
2 : String
3 : Boolean
4 : Array
5 : Value
6 : Object
7 : Whitespace
8 : null

0 comments: