Click to See Complete Forum and Search --> : Flash Actionscript Object Conversion Problem


scoper
05-09-2006, 07:01 PM
Ok,

basically... i have the following code:



/*
This is a string, this will always be a string... this must be a string!
*/
var obj_string:String = "{Name:'myName', Age:20}";

var obj = eval(obj_string);

trace(obj.Name);



Basically no matter what i try, the obj_string variable will not be converted into an actual object... its either a string or undefined.

The obj_string variable is defined here for simplicity, but it is being brought in from elsewhere

I have tried the following:

var o = eval(s);
var o = new Object(eval(s));
var o = new Object(s);
var o; eval("o = " + s);

I think thats about it...

a bit annoying because any of the obove will work in javascript ;P

Thanks for your help

CrazyMerlin
05-16-2006, 01:49 AM
well yes it is a string because you declared it as such.

if you need a data-type for multiple uses, you need to use a variant data-type.

scoper
05-16-2006, 05:46 AM
Actually, thats not correct, but i thank you for replying anyway. for example...


var o:Object = {Name:'Scott', Age:'25'}


works fine... i wanted to know why eval behaves differently in javascript and actionscript

schizo
05-16-2006, 10:45 AM
It was a string because he declared it as such, has quotes...
var obj_string:String = "{Name:'myName', Age:20}";

Also, the Object data type is variant and scoper was correct in saying that var o:Object = {Name:'Scott', Age:'25'} would work.

Nevertheless, the eval function in actionsript simply provides a way to access variables/movieclips dynamically. For instance:
var1 = "test";
trace(eval("var" + 1)); // outputs 'test'
To my knowledge there is no easy way to convert the original string "{Name:'myName', Age:20}" to an object (in actionscript). If you wanted, you could parse the string and construct the object yourself. I guess eval behaves differently across languages, becuase they are different languages.

CrazyMerlin
05-16-2006, 05:38 PM
So I wasn't wrong then...

I said it was string because you declared it as such...which is true!
I said you need to use a variant type....which you did using object...so that is true also!

So where was I wrong?

evaluation is performed on an operation to set a variable:


var o = eval(s);
var o = new Object(eval(s));
var o = new Object(s);
var o; eval("o = " + s);


so obviously none of that will evaluate as none of it is an operation.


Basically no matter what i try, the obj_string variable will not be converted into an actual object... its either a string or undefined.

Yes, because you declared it as a string, not an object!

scoper
05-16-2006, 05:41 PM
Dont take it personally... all i was getting at is that i have a string (a custom object definition, but in a string) and i wanted to convert it to an object... using eval works in javascript, and since javascript and actionscript are quite similar i was wondering if anyone knew why the eval statement works differently... chill out :p

CrazyMerlin
05-16-2006, 05:49 PM
hey, I didn't take it personally.

I was just pointing out that you were wrong!

lol

scoper
05-16-2006, 05:58 PM
Ok, well anyway, i ended up writing a basic object parser the code for which is at http://forums.devshed.com/flash-help-38/got-an-interesting-one-for-you-350363.html if anyones interested