如何将下载的带有字符串的.json文件解析为字符串变量?使用 as3corelib.swc。
问问题
28863 次
3 回答
19
我们开始吧,我当前项目中的完整示例:
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/
}
于 2012-10-12T13:58:23.147 回答
3
使用 as3corelib(即不是原生 JSON 类)解析 JSON 的函数是 'decode()'
JSON.decode( inputJson );
如果输入 json 编码正确,则字符串应该在结果对象中可用。如果字符串没有正确转义,您可能无法解析字符串,但这是输入数据的问题。
于 2012-10-12T02:59:58.833 回答
0
Adobe Animate 中的 actionscript 3.0,
var obj:Object = [
{
"capital":"Santiago",
"corregimientos":[
{
"santiago": "Capital of the province of Veraguas.",
"habitantes":"It has an approximate population of 50,877 inhabitants (2014).",
"area": "44.2 km²",
"limits": "It limits to the north with the district of San Francisco, to the south with the district of Montijo, to the east with the district of Atalaya and to the west with the district of La Mesa."
}
]
}
];
trace("Capital " + obj[0].capital+" Corrections: "+obj[0].corregimientos[0].santiago);
于 2021-06-12T21:14:36.153 回答