How can I get list of properties in an object in Actionscript?

2,068

Solution 1

If it's a dynamic object I believe you can just do something like this:

var obj:Object; // I'm assuming this is your object

for(var id:String in obj) {
  var value:Object = obj[id];

  trace(id + " = " + value);
}

That's how it's done in AS2, and I believe that still works for dynamic objects in AS3. I think the properties that it will show is more limited on non-dynamic objects.

Solution 2

flash.utils.describeType(value:*) will also give you a list of properties on an object.

Solution 3

You are probably looking for

ObjectUtil.getClassInfo(object) 

,see:

http://livedocs.adobe.com/flex/3/langref/mx/utils/ObjectUtil.html#getClassInfo%28%29

Be aware that there is a bug in it which causes it to treat XML as a non-dynamic data type. More on the bug in: bugs.adobe.com/jira/browse/SDK-17712

Solution 4

for me was useful only this:

trace('obj = '+getProperties(obj));

        public static function getProperties(obj:*):String  {
            var p:*;
            var res:String = '';
            var val:String;
            var prop:String;
            for (p in obj) {
                prop = String(p);
                if (prop && prop!=='' && prop!==' ') {
                    val = String(obj[p]);
                    if (val.length>10) val = val.substr(0,10)+'...';
                    res += prop+':'+val+', ';
                }
            }
            res = res.substr(0, res.length-2);
            return res;
        }

and you get something like this:

obj = m:email@ra..., r:true

Solution 5

// this method will work for retrieving properties of a *non-dynamic* (typed) object

// @return - all object properties
public function getProperties(_obj : *) : Array
{
        var _description : XML = describeType(_obj);
        var _properties : Array = new Array();
        for each (var prop:XML in _description.accessor)
        {
                var _property : Object = new Object();
                _property.name = String(prop.@name);
                _property.type = String(simple_type(prop.@type));
                _property.access = String(prop.@access);
                _property.declaredBy = String(prop.@declaredBy);
                try
                {
                   _property.value = _obj[_property.name];
                }
                catch (e : Error)
                {
                   _property.value = "";
                }
                _properties.push(_property)
        }
        _properties.sortOn("name");
        return _properties;
}

// better format for object class information
private function simple_type(_type : String) : String
{
        var lastIndex : int = _type.lastIndexOf("::");
        _type = lastIndex > 0 ? _type.substr(lastIndex + 2) : _type;
        return _type;
}
Share:
2,068
Swaggerboy
Author by

Swaggerboy

Updated on March 28, 2020

Comments

  • Swaggerboy
    Swaggerboy over 4 years

    My code is getting through the first two testcases, but failing the third one. Can someone help please?

    Link: https://www.hackerrank.com/challenges/tag-content-extractor

    Problem Statement:

    In a tag-based language like XML or HTML, contents are enclosed between a start tag and an end tag. Note that the corresponding end tag starts with a /.

    Given a string of text in a tag-based language, parse this text and retrieve the contents enclosed within sequences of well-organized tags meeting the following criterion:

    1. The name of the start and end tags must be same.

    2. Tags can be nested, but content between nested tags is considered not valid

    3. Tags can consist of any printable characters.

    Input Format:

    The first line of input contains a single integer, N (the number of lines). The N subsequent lines each contain a line of text.

    Constraints:

    • 1 <= N <= 100

    • Each line contains a maximum of 10000 printable characters.

    • The total number of characters in all test cases will not exceed 1000000.

    Output Format:

    For each line, print the content enclosed within valid tags. If a line contains multiple instances of valid content, print out each instance of valid content on a new line; if no valid content is found, print None.

    My code:

    import java.io.*;
    import java.util.*;   
    import java.text.*;    
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
    
        while(testCases > 0) {
            String line = in.nextLine();
            char[] A = line.toCharArray();
            String tag = "", tag1 = "";
            int a1 = 0, b1 = 0;
            int a = 0, b = 0;
            int flag = 0, end = 0;
    
            a = line.indexOf('<', a1);
            b = line.indexOf('>', b1);
            //System.out.println("Index of first '<' is " + a);
            //System.out.println("Index of first '>' is " + b);
    
            while ((a != -1) && (b != -1) && b < line.lastIndexOf(">")) {
                tag = "";
                tag1 = "";
                //System.out.println("Index of first '<' is " + a);
                //System.out.println("Index of first '>' is " + b);
                for (int k = a + 1; k < b; k++)
                    tag = tag + A[k];
                //System.out.println("tag is " + tag);
    
                a1 = line.indexOf('<', a + 1);
                b1 = line.indexOf('>', b + 1);
    
                if (A[a1+1] == '/') {
                    //System.out.println("Index of second '<' is " + a1);
                    //System.out.println("Index of second '>' is " + b1);   
                    for (int k = a1 + 2; k < b1; k++)
                        tag1 = tag1 + A[k];
                    if ((!tag.isEmpty()) && (!tag1.isEmpty())) {    
                        if (tag.equals(tag1)) {  
                            if ((b + 1) == a1) {
                                System.out.println("None");
                                flag = 1;
                            } else {
                                for (int k = b + 1; k < a1; k++)
                                    System.out.print(A[k]);
                                System.out.println();
                                flag = 1;
                            }
                        } else if (flag == 0) {
                            System.out.println("None");
                            flag = 1;
                        }
                    }   
                } 
                a = a1;
                b = b1;
                //System.out.println("tag1 is " + tag1);
            }
            if ((b == -1 || a == -1 || tag1.isEmpty() || tag.isEmpty()) && (flag == 0)) {
                System.out.println("None");
            }
            testCases--;
        }
     }
    }
    

    EDIT : For the test case #3 I'm not able to debug the issue for why that string which is large gets parsed line by line whereas it has to parse the whole paragraph! If it would parse that as a whole then i would get the output right.

    • OldProgrammer
      OldProgrammer about 8 years
      Well, did you step through each line of code in a debugger? Learning how to debug your program is just as important as learning java. I would spend some time on that.
    • Swaggerboy
      Swaggerboy about 8 years
      Learning it right now, Thanks for your suggestion. @schwobaseggl - Thanks for editing the code.