AS3 Load and access JSON data example


/ Published in: ActionScript 3
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.net.URLLoader;
  6. import flash.net.URLRequest;
  7.  
  8. import com.adobe.serialization.json.JSON;
  9.  
  10.  
  11. public class Main extends Sprite
  12. {
  13.  
  14. private var _jsonPath:String = "json_example.txt";
  15.  
  16. public function Main():void
  17. {
  18. if (stage) init();
  19. else addEventListener(Event.ADDED_TO_STAGE, init);
  20. }
  21.  
  22. private function init(e:Event = null):void
  23. {
  24. removeEventListener(Event.ADDED_TO_STAGE, init);
  25.  
  26. var loader:URLLoader = new URLLoader();
  27. var request:URLRequest = new URLRequest();
  28. request.url = _jsonPath;
  29. loader.addEventListener(Event.COMPLETE, onLoaderComplete);
  30. loader.load(request);
  31. }
  32.  
  33.  
  34. private function onLoaderComplete(e:Event):void
  35. {
  36. var loader:URLLoader = URLLoader(e.target);
  37. var jsonArray:Array = JSON.decode(loader.data);
  38.  
  39. trace("loader.data: " + loader.data);
  40.  
  41. trace(jsonArray[0].Id);
  42. // 7
  43.  
  44. trace(jsonArray[0].Mentions[0].Id);
  45. // 5
  46.  
  47. trace(jsonArray[0].Mentions[0].StatusId);
  48. // 34
  49.  
  50. trace(jsonArray[0].Mentions[0].CreatedDateTime);
  51. // /Date(1310051914617+0100)/
  52.  
  53. trace(jsonArray[0].Mentions[0].Text);
  54. // Text
  55.  
  56. trace(jsonArray[0].Mentions[0].UserName);
  57. // Username
  58.  
  59. trace(jsonArray[0].Mentions[0].UserLocation);
  60. // UK
  61.  
  62. trace(jsonArray[0].Mentions[0].UserLanguage);
  63. // en-GB
  64.  
  65. trace(jsonArray[0].Mentions[0].IsCheckIn);
  66. // true
  67.  
  68. trace(jsonArray[0].Tips[0].Id);
  69. // 5
  70.  
  71. trace(jsonArray[0].Tips[0].FourSquareId);
  72. // 7e06c4b2-1fb5-4f35-9619-c99a384f5ae7
  73.  
  74. trace(jsonArray[0].Tips[0].CreatedAt);
  75. // /Date(1310051914636+0100)/
  76.  
  77. trace(jsonArray[0].Tips[0].Text);
  78. // Text
  79.  
  80. trace(jsonArray[0].Tips[0].Username);
  81. // Username
  82.  
  83. trace(jsonArray[0].Checkins);
  84. // 0
  85.  
  86. trace(jsonArray[0].HereNow);
  87. // 0
  88.  
  89. trace(jsonArray[0].TimeStamp);
  90. // /Date(1310051914639+0100)/
  91.  
  92. trace(jsonArray[0].Venue.Id);
  93. // 7
  94.  
  95. trace(jsonArray[0].Venue.FoursquareId);
  96. // cacbf3bd-f0aa-403d-9f9b-2056b4985ba1
  97.  
  98. trace(jsonArray[0].Venue.Name);
  99. // Venue Name
  100.  
  101. }
  102.  
  103. }
  104. }
  105.  
  106.  
  107.  
  108. ////////////////////////////////////////////////////////
  109. // Here is the contents of the 'json_example.txt' file
  110. ////////////////////////////////////////////////////////
  111. /*
  112.  
  113. [
  114.   {
  115.   "Id": 7,
  116.   "Mentions": [
  117.   {
  118.   "Id": 5,
  119.   "StatusId": 34,
  120.   "CreatedDateTime": "\/Date(1310051914617+0100)\/",
  121.   "Text": "Text",
  122.   "UserName": "Username",
  123.   "UserLocation": "UK",
  124.   "UserLanguage": "en-GB",
  125.   "IsCheckIn": "true"
  126.   }
  127.   ],
  128.   "Tips": [
  129.   {
  130.   "Id": 5,
  131.   "FourSquareId": "7e06c4b2-1fb5-4f35-9619-c99a384f5ae7",
  132.   "CreatedAt": "\/Date(1310051914636+0100)\/",
  133.   "Text": "Text",
  134.   "Username": "Username"
  135.   }
  136.   ],
  137.   "Checkins": 0,
  138.   "HereNow": 0,
  139.   "TimeStamp": "\/Date(1310051914639+0100)\/",
  140.   "Venue": {
  141.   "Id": 7,
  142.   "FoursquareId": "cacbf3bd-f0aa-403d-9f9b-2056b4985ba1",
  143.   "Name": "Venue Name"
  144.   }
  145.   }
  146. ]
  147.  
  148. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.