Header Ads

Articale About JSON:


Articale About JSON:

JSON (JavaScript Object Notation):  JSON is text, written with JvavaScript Object Notation and JSON is a syntax for storing and Exchanging notation.
Where XML is arguably easier to read , but notoriously difficult to parse , JSON makes it breeze to store data in a format that machines dig. We can convert any JavaScript Object into JSON, and Send JSON to the Server. WE can also convert any joson received from the server into javascript objects. Once you encode the data with JSON , you can read it into a variable which creates and object .
When exchanging data between a server and a browser the data can only be text.
JSON Values:
String, number, an object(JSON object),an array , a Boolean, null.
In JSON, string values must be written with double quotes: such as {”name”=”Shamim”}
JSON Uses Java Scipt Syntax For example: person.name;

Sending Data :
 If you have data stored in a JavaScript object, You can  Convert object into JSON , and send it to a server .
**JSON data into and object in the DOM**
<div id=”placeholder”></div>
<script> var data={“firstName”:”Ray”};
Document.getElementById(“placeholder”).innnerHTML=data;
</script>
See the JSON Example on JS
<div id=”placeholder”></div>
<script>
Var data={“firstName”: ”Ray”,
“lastName”: ”Villalobos”,
“joined”:2012
};
Document.getElementById(“placeholder”).innerHTML=data.first;
</script>
First, You’ll See that we added a bit of formatting to our JSON data . That’s Just to make sure it’s easier to read for humans.
If you receive data in JSON format, you can convert it into a JavaScript object:
var myJSON= ‘{“fullname”:”shamim hossain”, “age”:31, “city”:”Dhaka” }’;
var myobj= JSON.parse (myJSON);
document.getElementByID(“demo”).innerHTML = myObj.name;

you can loop through object properties by using the for-in loop:
Example:   myObj={“name”:”shamim”, “age”:30, “BCI”:null};
 For (x in myObj){document.getElementById(“demo”).innnerHTML +=x;}


Objects in PHP can be converted into JSON by using the PHP function JSON_encode():
PHP file :
?php
$firstCode->name = “John”;
$firstCode->age  = 30;
$firstCode->city = “New York”;
$myJSON = json_encode($firstCode);
Echo $myJSON;
?>



** JSON from an external File :
<!DOCTYPE html>
               <html lang=”en”>
        <head>
             <meta charset=”utf-8” />
             <title>JSON Sample </title>
   <head>
<body>
               <div id=”placeholder”></div>
         <script  src=”http://code.jquery.com/jquery-1.7.1.min.js”>
        <script>
                       $.getJSON(‘data.json’, function(data)){
                        Var output=”<ul>”;
                  For(var i in data.users){ output+=”<ul>”+data.users[i].firstName + “”              +}
              Output+=”</ul>”;
             Document.getElemnetById(“placeholder”).innerHTML=output});
        </script>
           </body>
</html>




No comments

Disqus Shortname

Comments system

Powered by Blogger.