开发者

Convert loaded string to Object

开发者 https://www.devze.com 2023-04-03 14:15 出处:网络
In AS3, I want lo load a file text with URLLoader. In the file text I have the following string: {a:1,b:\"stri开发者_如何学运维ng\",c:[\"one\",\"two\"]}

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

{a:1,b:"stri开发者_如何学运维ng",c:["one","two"]}

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


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.


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.


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消