如何解析 JSON (AS3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12840523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse JSON (AS3)
提问by Dietrich
How to parse downloaded .json file with a string inside it to a string variable? With as3corelib.swc.
如何将下载的 .json 文件解析为字符串变量?使用 as3corelib.swc。
回答by kirushik
And here we go, full-working example from my current project:
我们开始了,来自我当前项目的完整示例:
protected function loadConfigFromUrl():void
{
var urlRequest:URLRequest = new URLRequest(CONFIG_URL);
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
}
}
private static function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
var data:Object = JSON.parse(loader.data);
trace("The answer is " + data.id+" ; "+data.first_var+" ; "+data.second_var);
//All fields from JSON are accessible by theit property names here/
}
回答by null
The function for parsing JSON using as3corelib ( ie not the native JSON class ) is 'decode()'
使用 as3corelib(即不是原生 JSON 类)解析 JSON 的函数是 'decode()'
JSON.decode( inputJson );
If the input json is properly encoded, strings should be available inside the resulting object. You may have trouble parsing strings if they have not been correctly escaped, but that is a problem with the input data.
如果输入的 json 被正确编码,字符串应该在结果对象中可用。如果字符串没有被正确转义,您可能会在解析字符串时遇到问题,但这是输入数据的问题。

