1 Reply Latest reply on Jun 16, 2010 9:51 AM by pradeep reddy

    How to convert an array collection instance to a complex object for interaction with webservice

    BertMcGee2

      Hi there,

       

      I have a stubborn problem that I am trying to work out the best way to solve the problem.  I am interacting with a WebService via HTTPService calling a method called find(String name) and this returns me a List of ComplexObjects that contain general string and int params and also lists of other Complex Objects.  Now using the code:

       

      ArrayCollection newOriginalResultsArray = new ArrayCollection(event.result as Array)

       

      flex converts my complex objects results to an arraycollection so that I can use it in datagrids etc.  Now up until this part is all good.  My problem is when getting a single instance from the results list, updating it by moving data around in a new datagrid for example - I want to interact with the webservice again to do an create/update.  This is where I am having problems - because these webservice methods require the complex object as a parameter - I am struggling to understand how I can convert the array collection instance back to my complex object without iterating over it and casting it back (maybe this is the only way - but I am hoping not).

       

      I am hoping that there is a simple solution that I am missing and that there is some smart cookie out there that could provide me with an answer - or at least somewhere to start looking. I guess if I have no other alternative - maybe I need to get the people who built the service to change it to accept an array - and let them do the conversion.

       

      Any help would be greatly appreciated.

       

      Bert

        • 1. Re: How to convert an array collection instance to a complex object for interaction with webservice
          pradeep reddy Level 1

          Hi Bert,

           

           

          According to my knowledge you can use describeType(Object) method which will return an XML... That XML will contain Properties and values just iterate through the XML and create a new Object..   Probably u can use this method...

           

          public function getObject(reqObj:Object,obj:Object,instanceName:String,name:String=null,index:int=-1):Obj ect
                  {
                      if(!reqObj)
                          reqObj = new Object();
                      var classInfo:XML = describeType(obj);
                      var className:String = instanceName;
                      if(name!=null)
                          className=name+"."+className;
                      if(index!=-1)
                          className=className+"["+index+"]";
                     
                      for each (var v:XML in classInfo..accessor)
                      {
                          var attributeName:String=v.@name;
                          var value:* = obj[attributeName]
                          var type:String = v.@type;
                         
                          if(!value)
                              reqObj[className+"."+attributeName] = value; 
                          else if(type == "mx.collections::ArrayCollection")
                          {
                              for(var i:int=0;i<value.length;i++)
                              {
                                  var temp:Object=value.getItemAt(i);
                                  getReqObject(reqObj,temp,attributeName,className,i);
                              }
                          }
                          else if(type == "String" || type == "Number" || type == "int" || type == "Boolean")
                              reqObj[ className+"."+attributeName] = value; 
                          else if (type == "Object")
                          {
                              for (var p:String in value)
                                  reqObj[ className+"."+attributeName+"."+p] = value[p];
                          }
                          else
                              getReqObject(reqObj,value,attributeName,className);
                      }
                     
                      return reqObj;
                  }

           

           

          Thanks,

          Pradeep