Return to Snippet

Revision: 48724
at July 8, 2011 05:33 by adrianparr


Initial Code
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"
    }
  }
]

*/

Initial URL

                                

Initial Description
For this to work you need the as3corelib classes from here ... https://github.com/mikechambers/as3corelib

Initial Title
AS3 Load and access JSON data example

Initial Tags
data, object, format, array, load, json

Initial Language
ActionScript 3