BaseTask.sendURLVariables How to return decoded JSON result as a specific Flex class?

659
1
01-16-2012 05:15 PM
DanielBaternik
Deactivated User
Hi all,

I have a class that extends BaseTask. I want the call to sendURLVariables() to return the decoded JSON as a specific class and not just an anonymous type of object which is what happens by default.

I.e.

Say i have a class called Entity with properties a,b and c. I'd like to be able to call a service, that returns this structure, and have Flex decode it as an Entity type, and not an anonymous type of object.  Is there a way to achieve this?

Any help would be greatly appreciated.

cheers
Tags (2)
0 Kudos
1 Reply
IvanBespalov
Frequent Contributor
Daniel,

may be this code helps you:

/**
 * Sample for Daniel
 */
package ee.alphagis
{
 import com.esri.ags.FeatureSet;
 import com.esri.ags.Graphic;
 import com.esri.ags.geometry.Geometry;
 import com.esri.ags.geometry.MapPoint;
 
 public class Entity
 {
  private var _aaa:String;
  private var _bbb:String;
  private var _point:MapPoint;
  
  public function get aaa():String
  {
   return _aaa;
  }
  
  public function get bbb():String
  {
   return _bbb;
  }
  
  public function get point():MapPoint
  {
   return _point;
  }
  
  public function Entity(a:String = null, b:String = null, g:MapPoint = null)
  {
   _aaa = a;
   _bbb = b;
   _point = g;
  }
  
  /**
   * @return string in JSON format
   */
  public function convertToJson():String
  {   
   var attr:Object = new Object();
   attr.a = _aaa;
   attr.b = _bbb;
   var gr:Graphic = new Graphic(_point, null, attr);
   
   // we need to create FeatureSet for Json converting
   var grArray:Array = new Array();
   grArray.push(gr);
   
   var fs:FeatureSet = new FeatureSet(grArray);
   fs.geometryType = Geometry.MAPPOINT;
   
   return fs.toJSON();
  }
  
  /**
   * Convert first feature in feature set to Entity
   * @param json is string in JSON format, json describes FeatureSet
   * @retun Entity
   */
  public static function entityFromJson(json:String):Entity
  {
   // Note the json describes FeatureSet not Graphic
   // Look and find in convertToJson() how to create FeatureSet or use API help
   if (json != null)
   {
    var fs:FeatureSet = FeatureSet.fromJSON(json);
    if (fs != null) 
    {
     // get array of graphics from feature set
     var arr:Array = fs.features as Array;
     if (arr != null && arr.length > 0)
     {
      // get graphic from array
      var gr:Graphic = arr[0] as Graphic;
      if (gr != null)
      {
       var gValue:MapPoint = gr.geometry as MapPoint;
       var aValue:String;
       var bValue:String;
       var attr:Object = gr.attributes;
       
       if (attr != null)
       {
        // get attributes from your graphic
        if (attr.hasOwnProperty("a"))
        {
         aValue = attr["a"].toString();
        }
        if (attr.hasOwnProperty("b"))
        {
         bValue = attr["b"].toString();
        }
       }
       
       return new Entity(aValue, bValue, gValue);
      }
     }
    }
   }
   
   return null;
  }
 }
}
0 Kudos