0

I was wondering if someone could explain or point me in the direction of how to implement an API that uses JSON in ActionScript 3.0. What I specifically want to know is how would I grab specific information. The following is how I do it in XML but I don't know how I would do something similar in JSON as in getting the equivalent of an XML tag. For example with the twitter API I'd like to grab the text https://dev.twitter.com/docs/api/1/get/search

        -----------XML Example:
        //create a new XML object with the XML
        xml = new XML(e.target.data);

        //This gives us an XMLList (an array) of <item> tags
        var all_items:XMLList = xml.channel.item;

        //loop through the <item> tags
        for (var i:uint = 0; i < all_items.length(); i++)
        {
            //get contents of title tag
            var titleText:String = all_items[i].title.text();

            //get contents of description tag
            var descriptionText:String = all_items[i].description.text();

            //get contents of link tag
            var linkText:String = all_items[i].link.text();

            //get contents of pubDate tag
            var dateText:String = all_items[i].pubDate.text();
        }

-------NEW CODE-----

public function onJSONLoaded(e:Event) {

trace("onJSONLoaded() called");

        json = JSON.decode(e.target.data);

        trace("json=" + json);
    } 

and the trace for it is

onJSONLoaded() called

json=[object Object]

barit
  • 113
  • 3
  • 10

1 Answers1

5

For working with JSON response, you can use this library https://github.com/mikechambers/as3corelib

Add as3corelib to your project, then in the event handler instead of XML you can give

var response:Object = JSON.decode(e.target.data);

then you will be able to access other properties like response.property

Diode
  • 24,570
  • 8
  • 40
  • 51
  • 2
    Or use [native JSON](http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/JSON.html) parsing in Flash Player 11 – RIAstar Jan 15 '12 at 09:34
  • Thank you for the link, but I'm still confused as to what I do with it? – barit Jan 15 '12 at 17:07
  • Tell me where you are stuck. Are you working with Flash Professional or Flash Builder ? Please give me more information so that I can help you :) – Diode Jan 15 '12 at 17:39
  • Thank you, I really appreciate it. I'm working with Flash Professional CS5. I downloaded the core library but I'm not really sure what to do next. Something I'd like to do is use this link(http://search.twitter.com/search.json?q=) as my request and then have the user enter a search term in flash(if they wanted to search web the link would be http://search.twitter.com/search.json?q=web) and then be able to get that search data back to flash. Hopefully what I'm asking makes sense to you. – barit Jan 15 '12 at 18:46
  • 1
    Have you added the library to Flash Professional ? Please open this page : http://active.tutsplus.com/tutorials/workflow/how-to-use-an-external-library-in-your-flash-projects/ and scroll down to "How to Set a Global Classpath" and follow the paragraph starting with "In later versions of Flash Professional, ... " and add the folder under "External Library Path" as shown in the image. – Diode Jan 15 '12 at 19:05
  • Thank you so much for the help, I've gotten my program at least functional now. I have a new problem in that when I trace the json data its not showing the actual data. I edited my original question to show you what I mean, and thanks again for all of the help. – barit Jan 16 '12 at 00:29
  • 1
    That is correct. Do like this to explore the `json` object. http://stackoverflow.com/questions/674158/flex-3-iterate-through-object-values – Diode Jan 16 '12 at 00:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6746/discussion-between-barit-and-diode) – barit Jan 16 '12 at 01:39