views:

773

answers:

2

Hi,

I built a class that parses JSON data, stores it in an array, and now I want to return that data so that it can be stores in an array in my root AS file. I'm eventually trying to pass the returned array to another class. My class looks like this:

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

 public class DataGrab extends Sprite
 {
  public var jsonData:Object;


  public function DataGrab()
  {

  }

  public function init(resource:String):void
  {
   var loader:URLLoader = new URLLoader();
   var request:URLRequest = new URLRequest(resource);
   loader.addEventListener(Event.COMPLETE, onComplete);
   loader.load(request);
  }

  private function onComplete(e:Event):void
  {
   var loader:URLLoader = URLLoader(e.target);
   jsonData = JSON.decode(loader.data);
     }

  public function getResults():Array
  {
   var people = jsonData.people;
   var names:Array = people.name;
   var payload  = new Array();
   var counter:Number = 0;
   for (var key:Object in names)
   {

    payload[counter] = [names[key].id, names[key].email];
    counter++;
   }
   return payload;
  }
 }
}

When I call the class in the root AS file, my code looks like:

var grabData:DataGrab = new DataGrab();
grabData.init(url:String);
var resultData:Array  = grabData.payload();

I keep getting the following error however:

1061: Call to a possibly undefined method payload through a reference with static type com.src.DataGrab.

Does anyone have advice on what might be wrong with my class, or a more logical way to write the getResults() function so that I can get retrieve the array being generated by this class?

Thanks.

+2  A: 

I would say the best thing to do is to just have your array as a public property, and dispatch an event when the data is ready.

I would also pass in the url string straight into the contructor, not to the init function. Seems like an unnecessary step to me.

I also don't see a method called "payload" in your code.

You should also use the reverse domain name convention for naming your class package.

TandemAdam
+1 error caused by missing payload method
bguiz
thanks, sorry about my negligence with the misnamed function. i think i need to take a short break from coding.
minimalpop
+4  A: 

Problem is you are trying to write your code synchronously but a network action is asynchronous. That is to say, you have to wait around for the network action to complete before requesting the data. Your best bet, as TandemAdam said is to use an event listener. Something like this:

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

 public class DataGrab extends Sprite
 {
  public var payload:Array;

  public function DataGrab()
  {
  }

  public function init(resource:String):void
  {
   var loader:URLLoader = new URLLoader();
   var request:URLRequest = new URLRequest(resource);
   loader.addEventListener(Event.COMPLETE, onComplete);
   loader.load(request);
  }

  private function onComplete(e:Event):void
  {
   var loader:URLLoader = URLLoader(e.target);
   var jsonData:Object = JSON.decode(loader.data);
   // Format the data
   var people = jsonData.people;
   var names:Array = people.name;
   var counter:Number = 0;
   payload = new Array();
   for (var key:Object in names)
   {
    payload[counter] = [names[key].id, names[key].email];
    counter++;
   }
   dispatchEvent(new Event(Event.COMPLETE));
  }

  public function getResults():Array
  {
    return payload;
  }
 }
}

Then you can use it like this:

var grabData:DataGrab = new DataGrab();
function dataReadyHandler(event:Event):void {
    var grabData:DataGrab = event.target as DataGrab;
    trace(grabData.getResults());
    // or
    trace(grabData.payload);
}
grabData.addEventListener(Event.COMPLETE, dataReadyHandler);
grabData.init(url:String);

I strongly suggest reading more about the EventDispatcher class and asynchronous programming. This may help also http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs%5FParts&file=00000838.html

Jotham
+1. I would definitly recomend reading that livedocs link. Also this one is quite helpful http://gamedev.michaeljameswilliams.com/2009/02/06/using-as3-event-listeners/
TandemAdam