AS3:创建用于存储JSON数据的对象数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3:创建用于存储JSON数据的对象数组相关的知识,希望对你有一定的参考价值。
Notes to self: re-use this for other projects
private var _myObjArr:Array = new Array(); // array of objects private var _stringAuthor:String; private var _stringDate:String; private var _stringText:String; /** * Success handler after requesting JSON data from server * * @param event Event * @return void */ private function onSuccess(event:Event):void { var loader:URLLoader = event.target as URLLoader; var data:Object = null; try { // get the raw JSON data data = JSON.decode(loader.data); } catch(e:Error) { trace("Error requesting document"); } loader.close(); loader = null; if (data !=null) { if (data.error) { trace(data.error); } else { // loop thru data for (var key:Object in data) { _stringAuthor = '<p><span class="author">' + data[key].author + '</span></p>'; _stringDate = '<span class="date">' + data[key].date + '</span></p>'; _stringText = '<p><span class="text"> "' + data[key].text + '"</span></p>'; // save Author, Date, Text to an object array _myObjArr var obj:Object = new Object(); obj.Author = _stringAuthor; obj.Date = _stringDate; obj.Text = _stringText; _myObjArr.push(obj); } } } } // end onSuccess // now that we got all our info saved, we can retrieve it later in other functions like: for(var i:uint = 0; i < _myObjArr.length; i++) { _blogPost[i] = addBlogPosts(); addChild(_blogPost[i]); } // does this snippet help anyone?
以上是关于AS3:创建用于存储JSON数据的对象数组的主要内容,如果未能解决你的问题,请参考以下文章
用于存储子文档的 JSON 对象数组的 Mongoose 模式
错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”