JSON (JavaScript Object Notation) describes data objects using JavaScript syntax, but JSON is still independent of the language and platform. The JSON parser and JSON library support many different programming languages. JSON data can be transmitted using AJAX. The MIME type of JSON text is "application/json ".
JSON syntax rules
JSON syntax is a subset of the syntax of JavaScript Object Notation.
L data in name/value pairs
L data is separated by commas (,).
L brackets save objects
L square brackets Save the Array
The JSON value can be:
L number (integer or floating point number)
L string (in double quotation marks)
L logical value (true or false)
L Array (in square brackets)
L object (in curly brackets)
L null
JSON example
var txt = '{"employees" : [' +'{"firstName":"Bill" , "lastName":"Gates"},' +'{"firstName":"George" ,"lastName":"Bush" },' +'{"firstName":"Thomas" ,"lastName":"Carter" } ]}';
Because the JSON syntax is a subset of JavaScript syntax, the JavaScript function eval () can be used to convert JSON text to JavaScript objects.
The eval () function uses a JavaScript compiler that parses JSON text and generates JavaScript objects. Text must be enclosed in parentheses to avoid syntax errors:
var obj = eval("(" + txt + ")");
You can also directly create a JSON object:
var jsonObject ={"manager":"Tom","employees": [ { "firstName":"Bill" ,"lastName":"Gates" }, { "firstName":"George" ,"lastName":"Bush" }, { "firstName":"Thomas" ,"lastName":"Carter" } ]}
Obtain data in JSON:
Get the manager name: jsonObject. manager
Obtain the name of the second employee: jsonObject. Employees [1]. lastName