user840248
user840248

Reputation:

Convert loaded string to Object

In AS3, I want lo load a file text with URLLoader. In the file text I have the following string:

{a:1,b:"string",c:["one","two"]}

Is it possible (once loaded) to convert it to an Object?

Upvotes: 0

Views: 1556

Answers (3)

Pham Hoang Hiep
Pham Hoang Hiep

Reputation: 1

Your string is a sting with JSON format. Use JSONDecoder to decode it to an Object, like this:

var dc:JSONDecoder = new JSONDecoder("{a:1,b:'string',c:['one','two']}");
var ob:Object = dc.getValue();

Upvotes: 0

The_asMan
The_asMan

Reputation: 6403

What you need is to eval the string to create the object.
This is done natively in javascript and AS2. AS3 however does not support this function.
But all is not lost. The people at Hurlant have created a library that does this "almost" as good as native JavaScript.
Here is a good example.
And another library example using d.eval

I would like to point out though that if you have accept to the source of the object string that you create a JSON object out of it. The JSON libraries are usually much easier and more reliable to use then the libraries that do Eval.

Upvotes: 0

meddlingwithfire
meddlingwithfire

Reputation: 1437

There is no intrinsic deserializer built into the language, no. But if your text file sticks to the JSON standard, then you could use a JSON parser to do the conversion for you: http://code.google.com/p/as3corelib/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom%2Fadobe%2Fserialization%2Fjson

Or, if you cannot adhere to JSON, you could always write your own deserializer.

Upvotes: 1

Related Questions