Wednesday 3 April 2013

Encode and Decode JSON Object using Sencha Touch 2

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is used primarily to transmit data between a server and web application, serving as an alternative to XML. Today, we are going to see how we can encode and decode Objects in Sencha Touch 2

Encode JSON Object

Ext.JSON.encode() is used to Encodes an Object, Array or other values. It accepts Object (the value to encode) as the parameter and returns JSON String.

  var obj = {
      name:"suresh",       age: "30",       qualification: "B.Tech",       location: "India",       hobbies: ["reading books","playing games","watching movies"]   };   var encoded = Ext.JSON.encode(obj);   console.log("Encoded Output: "+encoded);

Decode JSON Object

Ext.JSON.decode() is used to decodes (Parses) the JSON string to object. It accepts String (the JSON String) and Boolean (Whether to return null or throw an exception when JSON is invalid) as the parameters and returns Object/NULL.
 var str = '{"name":"suresh","age": "30","qualification": "B.Tech","location": "India","hobbies": ["reading books","playing games","watching movies"]}';

 var decoded = Ext.JSON.decode(str);
 console.log("Decoded Output: "+decoded);

Hope, you enjoyed this Post.

No comments:

Post a Comment