ActionScript 3 AS3加载并访问JSON数据示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3加载并访问JSON数据示例相关的知识,希望对你有一定的参考价值。

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import com.adobe.serialization.json.JSON;
	

	public class Main extends Sprite 
	{
		
		private var _jsonPath:String = "json_example.txt";
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest();
			request.url = _jsonPath;
			loader.addEventListener(Event.COMPLETE, onLoaderComplete);
			loader.load(request);
		}
		
		
		private function onLoaderComplete(e:Event):void 
		{
			var loader:URLLoader = URLLoader(e.target);
			var jsonArray:Array = JSON.decode(loader.data);
			
			trace("loader.data: " + loader.data);

			trace(jsonArray[0].Id);
			// 7
			
			trace(jsonArray[0].Mentions[0].Id);
			// 5
			
			trace(jsonArray[0].Mentions[0].StatusId);
			// 34
			
			trace(jsonArray[0].Mentions[0].CreatedDateTime);
			// /Date(1310051914617+0100)/
			
			trace(jsonArray[0].Mentions[0].Text);
			// Text
			
			trace(jsonArray[0].Mentions[0].UserName);
			// Username
			
			trace(jsonArray[0].Mentions[0].UserLocation);
			// UK
			
			trace(jsonArray[0].Mentions[0].UserLanguage);
			// en-GB
			
			trace(jsonArray[0].Mentions[0].IsCheckIn);
			// true
			
			trace(jsonArray[0].Tips[0].Id);
			// 5
			
			trace(jsonArray[0].Tips[0].FourSquareId);
			// 7e06c4b2-1fb5-4f35-9619-c99a384f5ae7
			
			trace(jsonArray[0].Tips[0].CreatedAt);
			// /Date(1310051914636+0100)/
			
			trace(jsonArray[0].Tips[0].Text);
			// Text
			
			trace(jsonArray[0].Tips[0].Username);
			// Username
			
			trace(jsonArray[0].Checkins);
			// 0
			
			trace(jsonArray[0].HereNow);
			// 0
			
			trace(jsonArray[0].TimeStamp);
			// /Date(1310051914639+0100)/
			
			trace(jsonArray[0].Venue.Id);
			// 7
			
			trace(jsonArray[0].Venue.FoursquareId);
			// cacbf3bd-f0aa-403d-9f9b-2056b4985ba1
			
			trace(jsonArray[0].Venue.Name);
			// Venue Name
			
		}

	}
}



////////////////////////////////////////////////////////
// Here is the contents of the 'json_example.txt' file
////////////////////////////////////////////////////////
/*

[
  {
    "Id": 7,
    "Mentions": [
      {
        "Id": 5,
        "StatusId": 34,
        "CreatedDateTime": "\/Date(1310051914617+0100)\/",
        "Text": "Text",
        "UserName": "Username",
        "UserLocation": "UK",
        "UserLanguage": "en-GB",
        "IsCheckIn": "true" 
      }
    ],
    "Tips": [
      {
        "Id": 5,
        "FourSquareId": "7e06c4b2-1fb5-4f35-9619-c99a384f5ae7",
        "CreatedAt": "\/Date(1310051914636+0100)\/",
        "Text": "Text",
        "Username": "Username"
      }
    ],
    "Checkins": 0,
    "HereNow": 0,
    "TimeStamp": "\/Date(1310051914639+0100)\/",
    "Venue": {
      "Id": 7,
      "FoursquareId": "cacbf3bd-f0aa-403d-9f9b-2056b4985ba1",
      "Name": "Venue Name"
    }
  }
]

*/

以上是关于ActionScript 3 AS3加载并访问JSON数据示例的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3 - 从本地驱动器加载并在闪存中显示图像

ActionScript 3 AS3:图像加载

ActionScript 3 AS3预加载器

ActionScript 3 AS3:XML加载

ActionScript 3 AS3加载Txt

ActionScript 3 AS3:预加载器