6

What I want to do is make a get request to this URL: http://api.beatport.com/catalog/3/most-popular, which should return some JSON and then parse out certain information from it.

How would I go about doing this in Actionscript 3? I'm more concerned with figuring out how to get the data to feed to a JSON parser rather than parsing the JSON, since there seem to be plenty of questions about parsing JSON. The reason I want to do this in AS3 is that I have a 3D flash visualization set up and I want to get this data, parse out the relevant bits, and then display the parsed bits in the visualization.

I'm open to any other ways of doing this that can easily be integrated with Flash besides AS3 if there's an easier way to do it in another language.

Saliceran
  • 330
  • 1
  • 7
  • 26

3 Answers3

12
  1. Add the corelib.swc to your library path.

  2. Import the JSON library: import com.adobe.serialization.json.JSON;

  3. Call your service with code something like this:

    var request:URLRequest=new URLRequest();
    request.url=YOUR_ENDPOINT
    request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
    request.method=URLRequestMethod.GET;
    var loader:URLLoader=new URLLoader();
    loader.addEventListener(Event.COMPLETE, receive);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, notAllowed);
    loader.addEventListener(IOErrorEvent.IO_ERROR, notFound);
    loader.load(request);
    
    protected function receive(event:Event):void
    {
         var myResults:Array=JSON.decode(event.target.data);
    }
    
  4. Parse the results with JSON.decode(results).

as3corelib is maintained here: https://github.com/mikechambers/as3corelib#readme.

Matt Garland
  • 437
  • 1
  • 4
  • 11
  • Actually my main question was how exactly do I get the JSON so that I can parse it. – Saliceran Apr 15 '12 at 21:15
  • Once the JSON is in that array how do I go about getting out the the text I need. I only need specific pieces of data from it. It holds information about 10 different songs and for each song I want the name of the song, the artist, the genre and the record label. I've never worked with JSON or Actionscript. – Saliceran Apr 21 '12 at 21:53
  • 1
    JSON decodes into objects in AS3. There will be associated arrays with this syntax: {someProp:someValue, someOtherProp:someOtherValue, and regular arrays with this syntax: [someValue, someOtherValue].You will need to walk the structure with a combination of for loops. To iterate through properties of an object or associated array use the for-in loop: for (var prop:String in object) {trace (object[prop]}. To iterate through an array use a for-each loop: for each (var arrayItem:Object in someArray) {trace (someItem.someProperty)}. Object.hasOwnProperty("someProp") confirms a property exists. – Matt Garland Apr 22 '12 at 04:32
  • It gives me an error when I define myResults as an Array because it says it can't change an Object into an Array. So, I changed it to Object and used the object trace for loop you provided and all it prints out is [object Object] for each thing. Am I doing something wrong? Here is the json [link](http://api.beatport.com/catalog/3/most-popular?perPage=20) I want to grab information from. I only want certain parts of it. – Saliceran Apr 24 '12 at 16:42
  • 1
    If you are picking out an individual property, you can access it in various ways: (assuming a top-level object): myResult[someProperty][someProperty][someProperty], or if one of the nodes is an array, you can mix in array access: myResult[someProperty][myIndex][someProperty]. Like, myResult["metadata"]["propPointsToArray"][3]["arrayItemProp"]. – Matt Garland Apr 25 '12 at 01:10
  • 1
    [object Object] is what toString() prints out for a vanilla object. If you want to iterate through the props themselves, trace out prop in a for-in loop. If you iterate through the property value at the top of the tree, object[prop] will most likely be another object rather than a string, boolean or number. – Matt Garland Apr 25 '12 at 01:14
11

Alternatively if you're using Flash Player 11 or AIR 3.0 or higher you can use the built in JSON object to decode your JSON. It's a top level object so you dont even need to import anything, just do:

var decoded : Object = JSON.parse(loadedText);

See: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html

plemarquand
  • 1,866
  • 2
  • 16
  • 20
3

I believe the as3corelib has a JSON serializer and deserializer

You can use those instead of re-inventing the wheel and writing parsing logic afresh.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70