• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Flex Json Encode ...

Here you will know how to decode a JSON-formate string to an object, JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Here is the method:

   1. <?xml version="1.0" encoding="utf-8"?>
   2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
   3.     <mx:Style>
   4.         global
   5.         {
   6.             fontSize:14;
   7.         }
   8.     </mx:Style>
   9.     <mx:Script>
  10.         <![CDATA[
  11.             import flash.utils.describeType;
  12.             import mx.utils.ObjectUtil;
  13.             import com.adobe.serialization.json.JSON;
  14.           
  15.             private var myObject:Object;
  16.           
  17.             [Bindable]
  18.             private var encodedObjectString:String;
  19.           
  20.             [Bindable]
  21.             private var decodedObjectString:String;
  22.           
  23.             private function initApp():void
  24.             {
  25.                 myObject = new Object();
  26. 
  27.                 myObject.id = "userCollection";
  28.                 myObject.users = new Array();
  29.               
  30.                 for(var i:uint = 0 ;i < 2;i++)
  31.                 {
  32.                     var user:Object = new Object();
  33.                     user.name = "user" + i;
  34.                     user.password = "password" + i;
  35.                     user.login = i%2==0? true : false;
  36.                     user.role = new Object();
  37.                     user.role.name = "user";
  38.                     user.flags = new Array(1,2,3,4,5,6);
  39.                     (myObject.users as Array).push(user);
  40.                 }
  41.               
  42.                 myObject.userCount = (myObject.users as Array).length;
  43.                 myObject.extension = null;
  44.                 myObject.flags = new Array("a","b","c",false,1,5,77,null);
  45.               
  46.                 /* ************** encode myObject to JSON string ************** */
  47.                 encodedObjectString = JSON.encode(myObject);
  48.                 /* ************************************************************ */
  49.               
  50.                 decodedObjectString = getDecodedObjectString(myObject);
  51.             }
  52.           
  53.             private function getDecodedObjectString(obj:Object,level:int = 0):String
  54.             {
  55.                 var prefix:String = "";
  56.                 for(var i:int = 0;i < level; i++)
  57.                 {
  58.                     prefix += "    ";
  59.                 }
  60.                 level++;
  61.               
  62.                 var retValue:String = "";
  63.                 var objProperties:Object = ObjectUtil.getClassInfo(obj);
  64.               
  65.                 if(objProperties.properties.length>0)
  66.                 {
  67.                     for each(var qname:QName in objProperties.properties)
  68.                     {
  69.                         var property:* = obj[qname.localName];
  70.                         var propertyType:String = getQualifiedClassName(property);
  71.                       
  72.                         switch(propertyType)
  73.                         {
  74.                             case "null":
  75.                                 retValue += prefix + qname.localName + "(" + propertyType + ")" + ":" + property + " ";
  76.                                 break;
  77.                             case "String":
  78.                                 retValue += prefix + qname.localName + "(" + propertyType + ")" + ":" + """ + property + """ + " ";
  79.                                 break;
  80.                             case "int":
  81.                                 retValue += prefix + qname.localName + "(" + propertyType + ")" + ":" + property + " ";
  82.                                 break;
  83.                             case "Array":
  84.                                 retValue += prefix + qname.localName + "(" + propertyType + ")" + ":" + " " + getDecodedObjectStringFromArray(property,level) + " ";
  85.                                 break;
  86.                             case "Object":
  87.                                 retValue += prefix + qname.localName + "(" + propertyType + ")" + ":" + " " + getDecodedObjectString(property,level) + " ";
  88.                                 break;
  89.                             case "Boolean":
  90.                                 retValue += prefix + qname.localName + "(" + propertyType + ")" + ":" + property + " ";
  91.                                 break;
  92.                         }
  93.                     }
  94.                 }
  95.                 return retValue;
  96.             }
  97.           
  98.             private function getDecodedObjectStringFromArray(arr:Array,level:int):String
  99.             {
 100.                 var prefix:String = "";
 101.                 for(var i:int = 0;i < level; i++)
 102.                 {
 103.                     prefix += "    ";
 104.                 }
 105.                 level++;
 106.               
 107.                 var retValue:String = "";
 108.                 var index:int = 0;
 109.               
 110.                 for each(var value:* in arr)
 111.                 {
 112.                     var type:String = getQualifiedClassName(value);
 113.                   
 114.                     switch(type)
 115.                     {
 116.                         case "null":
 117.                             retValue += (index == 0?prefix:"")  + "(" + type + ")" + value + ",";
 118.                             break;
 119.                         case "String":
 120.                             retValue += (index == 0?prefix:"")  + "(" + type + ")" + value + ",";
 121.                             break;
 122.                         case "int":
 123.                             retValue += (index == 0?prefix:"")  + "(" + type + ")" + value + ",";
 124.                             break;
 125.                         case "Array":
 126.                             retValue += prefix + "(" + type + ")" + getDecodedObjectStringFromArray(value,level);
 127.                             break;
 128.                         case "Object":
 129.                             retValue += prefix +  "(" + type + ")" + " " +getDecodedObjectString(value,level);
 130.                             break;
 131.                         case "Boolean":
 132.                             retValue += (index == 0?prefix:"")  + "(" + type + ")" + value + ",";
 133.                             break;
 134.                     }
 135.                   
 136.                     index++;
 137.                 }
 138.                 return retValue;
 139.             }
 140.           
 141.             private function updateDecodedObjectString():void
 142.             {
 143.                 try
 144.                 {
 145.                     /* ************** decode JSON string to an object ************** */
 146.                     myObject = JSON.decode(encodedString.text);
 147.                     /* ************************************************************* */
 148.                     decodedObjectString = getDecodedObjectString(myObject);
 149.                 }
 150.                 catch(e:Error)
 151.                 {
 152.                   
 153.                 }
 154.             }
 155.         ]]>
 156.     </mx:Script>
 157.     <mx:Panel title="JSON Demo" width="100%" height="100%" layout="absolute">
 158.         <mx:TextArea id="encodedString" text="{encodedObjectString}" width="50%" height="100%" right="0" top="0" change="updateDecodedObjectString()"/>
 159.         <mx:VBox width="50%" left="0" height="100%">
 160.             <mx:Label text="object properties" fontWeight="bold"/>
 161.             <mx:TextArea editable="false" height="100%" width="100%" text="{decodedObjectString}"/>
 162.         </mx:VBox>
 163.     </mx:Panel>
 164. </mx:Application>

 Original Source:

AddThis Social Bookmark Button

Posted at 03:49:28 pm | Permalink | Posted in JSON  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange