A brief introduction to JSON, JSON (web Effects object notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy to machine parse and generate. It is based on JavaScript programming language, standard ecma-262 a subset of 3rd Edition-december 1999. JSON uses a completely language-independent text format, but it also uses a family of C-language (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language.
JSON is constructed in two structures:
A collection of name/value pairs (a collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).
The ordered list of values (an ordered list of values). In most languages, it is understood as an array.
PHP File code
<?php
$res [' id '] = $_post[' id '];
$res [' name '] = "elar";
$res [' age '] = "21";
$response = "Hello this is response". $_post[' id '];
echo Json_encode ($res);
?>
JS Code
<script type= "Text/javascript" >
function Getjson () {
var xmlhttp;
try {
Firefox, Opera 8.0+, Safari
XMLHTTP = new XMLHttpRequest ();
}
catch (e) {
Internet Explorer
try {
XMLHTTP = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (e) {
try {
XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (e) {
Alert ("Your browser does not support ajax! ");
return false;
}
}
}
Xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate = = 4) {
//alert (xmlhttp.responsetext);
var str = xmlhttp.responsetext;
document.getElementById (' Show '). innerHTML +=str;
//alert (str);
var obj = eval (' + xmlhttp.responsetext + ') ');
//var obj = eval ({"id": "123", "Name": "Elar", "Age": "21"}));
Alert (obj.name);
}
}
var data = "id=123";
Xmlhttp.open ("Post", "testjson.php", true);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("id=123");
}
</script>
<input type= button "onclick=" Getjson () "value=" Press me! "/>
<div id=" show "></DIV>