Thursday, April 21, 2011

ActionScript 3 Object to name value string

In a Flex application I am trying to turn an Object into a QueryString such as name1=value1&name2=value2... But I am having trouble getting the names of the Objects children. How do I enumerate the names instead of the values?

Thanks

From stackoverflow
  • I'm guessing you're doing a for each(in) loop. Just do a normal for(in) loop and you'll get the names instead of the values:

    for(var name:String in obj) {
      var value:* = obj[name];
      // do whatever you need
    }
    
    Sophistifunk : If it's a concrete class instead of just an Object, see ObjectUtil.getClassInfo()
    Herms : I believe `for(in)` still works on concrete classes, but it's generally more limited on what it returns. It will only pick up what those classes have set up as visible using the `setPropertyIsEnumerable` method.
  • Ok, first off, if you need that query string to actually query a server, you don't really need to get it yourself as this code will query the server for you

    protected function callSerivce():void
    {
        var o:Object = new Object();
        o.action = "loadBogusData";
        o.val1 = "dsadasd";
        service.send(o);
    }
    
    <mx:HTTPService id="service" url="http://www.somewhere.com/file.php" method="GET" showBusyCursor="true"/>
    

    Will make a call to the server like this: http://www.somewhere.com/file.php?action=loadBogusData&val1=dsadasd

    But in case you really want to analyze the object by hand, try using ObjectUtil.getClassInfo, it returns a lot of information including all the fields (read more on LiveDocs).

0 comments:

Post a Comment