Monday, February 28, 2011

Experiencing JSON with Flash AS 3.0.


Hello friends,
Main thing about JSON is IT’S NOT WELL KNOWN.
Now  its time to know about it.

What you will learn..
JSON and its structure.
Actual use of JSON in Flash Projects.
Advantage over XML

What you should know..
XML, its structure and use.

What is JSON
JSON is JavaScript Object Notation a light weight data exchange format based on JavaScript Language.
It is a simple text format which is language independent and easy to use for many languages as ASP, PHP, ActionScript, C ,C++, Java, Ruby, Pearl and many more…

Structure of json
Objects:  Objects begin with opening curly braces “{” and end with closing  curly braces ”}”.
Ex. {Object}
Object members: Object Members consist of strings and values, separated by colon (:).
Members are separated by commas.
Ex.
 {
“Name”:”Chinu”,
“Contact”:1234567890
}
Arrays: Arrays begin with “[” braces and end with “]” braces and contain values which are separated by commas. Value can be a string, a number, an object, an array, or Boolean value or null.
Strings: Strings are surrounded by double quotes.
A simple example of JSON:
{
  "Employee": {
    "Name":”Chinu”,
    "Eid":151,
    "Designation":"Developer",
    "Address":
    {
      "City":"Pune",
      "Country": “India”,
      "Pin": 100100
    },
  "WorkArea":[“Design” , ”Development” , ”Testing”]
  }
}
Here,
 Employee is a top-level object; all other data is a member of that object. Name, Eid and Designation are all simple members of Employee Object having number and string data. Address is a nested object, having members as City , Country, Pin. WorkArea is also simple member of Employee which is an array, containing String values.

Comparing with xml
As compare to XML, JSON is very easy to use and read/write for human and machine as it contain less grammar/syntax.
JSON does not use [CDATA[]] feature to represent html data.
XML is document oriented Markup Language so it is better to use as document exchange format,
JSON is data oriented language so better to use as data exchange format.
XML is adopted by industry as it is widely known than JSON.
There are lot of advantages of JSON over XML regarding Data types supports, memory management, browser security, faster execution, data serialization/ deserialization etc.
Many web services(like Google, yahoo, flickr etc) use JSON instead of XML.
JSON ex.
{
“Employee”:{
“Name”:”Chinu”,
“Eid”:101,
“Add”:{
“City”:”Pune”,
“Country”:”India”
}
}
}
Same JSON Ex. In simple XML format is represented as,
<?xml version="1.0" ?>
<EmpDetails>
< Employee >
<Name>Chinu</Name>
<Eid>101</Eid>
<Add>
<City>Pune</City>
<Country>India</Country>
</Add>
</ Employee >
</ EmpDetails >
 

JSON with  ActionScript
JSON is very useful in flash projects. As per my experience , I use it in php where I used to create dynamic XML. But while loading thousands clip arts I need to wait for some time to parse and traverse XML for my Flash Design Tool.
Here is simple example as,
Here is my ActionScript Code as,
import com.adobe.serialization.json.JSON;
var urlldr:URLLoader=new URLLoader();
urlldr.load(new URLRequest('sample.txt'));
urlldr.addEventListener(Event.COMPLETE,jsonload);
function jsonload(e:Event):void
{
                var JSONData:Array = JSON.decode(e.target.data);
                for(var i=0;i< JSONData.length;i++)
                trace(JSONData [i].number);
}


Out put is,
12345
67890

sample.txt file contain data as,
 [ {"name":"Sachin", "number":"12345"}, {"name":"Vidya", "number":"67890"}]
Once I loaded simple.txt file I got data in that file.
Then I decode all text to JSON format, here data is validate.
Then all JSON data is collected in an Array for later use.
You can encode any JSON object as String.
var myString:String = JSON.encode( myValue );
 
other pretty thing I found is you can direct compare two JSON objects as,
see below function 
 
function compareObj (obj1:Object, obj2:Object): Boolean {
      try {
               var value1: String = JSON.encode(obj1);
               var value2: String = JSON.encode(obj2);
      }
      catch (e:JSONParseError) {
               trace('Error : '+ e.text);
               return false;
      }
      return (value1 === value2);
  }
 
It is very difficult to compare two XMLs or its nodes.

Lets Spread JSON.

Here are some useful links:
JSON validator
JSON home
JSON library for ActionScript
https://github.com/mikechambers/as3corelib/

Check out this article in FFD Magazine February  Issue.

No comments:

Post a Comment