Introduction
JSON is a data format for transferring data as an alternative to XML. JSON is ligter than XML. The syntax is like mapping.
Program Code
var data = {
"name": "Lady Gaga",
"city": "New York",
"age": 24
}
"name": "Lady Gaga",
"city": "New York",
"age": 24
}
Accessing And Modifying
For accessing JSON data, we use dot operator. For above example, we useProgram Code
var the_name = data.name;
var the_cityy = data.city;
var the_age = data.age;
alert("Name: "+ the_name +"\nCity: "+ the_city +"\nAge: "+ the_age)
var the_cityy = data.city;
var the_age = data.age;
alert("Name: "+ the_name +"\nCity: "+ the_city +"\nAge: "+ the_age)
Output
Name: Lady Gaga
City: New York
Age: 24
City: New York
Age: 24
Program Code
data.name = "Jenifer Lopez";
alert("Name: "+ data.name);
alert("Name: "+ data.name);
Output
Name: Jenifer Lopez
Transfering JSON Data Through Ajax
JSON is very suitable for transferring data from client to server. The first step, you encode your data to JSON format, then sending it through AJAX. After it arrived on the server side, then processing it as server code. Here's we will use PHP as server code.Program Code
<div>Name: <input type="text" id="name"/></div>
<div>City: <input type="text" id="city"/></div>
<input type="button" onclick="sending()" value="Send Data"/>
<script>
function sending(){
var name = document.getElementById('name').value;
var city = document.getElementById('city').value;
var data = {"name":name, "city":city};
var ajax = new XMLHttpRequest();
var json_data = JSON.stringify(data);
ajax.open("GET", "hello.php?data="+ json_data, false);
ajax.send();
}
</script>
<div>City: <input type="text" id="city"/></div>
<input type="button" onclick="sending()" value="Send Data"/>
<script>
function sending(){
var name = document.getElementById('name').value;
var city = document.getElementById('city').value;
var data = {"name":name, "city":city};
var ajax = new XMLHttpRequest();
var json_data = JSON.stringify(data);
ajax.open("GET", "hello.php?data="+ json_data, false);
ajax.send();
}
</script>
On the server side, you need to write this code.
Program Code: hello.php
$json_data = $_GET['data'];
// conver the json data format to associative array
$data = json_decode($json_data);
// get the name and city
$name = $data["name"];
$city = $data["city"];
// conver the json data format to associative array
$data = json_decode($json_data);
// get the name and city
$name = $data["name"];
$city = $data["city"];
Handling JSON Data From Server
It's also convenient to handling the received data from the server. Here's a practical example using PHP. Write this code in hello.phpProgram Code: hello.php
$data = {
"name" => "lady gaga",
"city" => "new york"
}
$json_data = json_oncode($data);
echo $json_data;
"name" => "lady gaga",
"city" => "new york"
}
$json_data = json_oncode($data);
echo $json_data;
On the client side, the code is:
Program Code
<h1>Name: <span id="name"></span></h1>
<h1>City: <span id="city"></span></h1>
<input type="button" onclick="load()" value="Load JSON Data"/>
<script>
function load(){
var name = document.getElementById('name');
var city = document.getElementById('city');
var ajax = new XMLHttpRequest();
ajax.send("GET", "hello.php", false);
var data = eval('('+ ajax.responseText +')');
name.innerHTML = data.name;
city.innerHTML = data.city;
}
</script>
<h1>City: <span id="city"></span></h1>
<input type="button" onclick="load()" value="Load JSON Data"/>
<script>
function load(){
var name = document.getElementById('name');
var city = document.getElementById('city');
var ajax = new XMLHttpRequest();
ajax.send("GET", "hello.php", false);
var data = eval('('+ ajax.responseText +')');
name.innerHTML = data.name;
city.innerHTML = data.city;
}
</script>
No comments:
Post a Comment