1 Reply Latest reply on Jul 19, 2009 7:21 PM by musicofnight

    Question about JSON/ArrayCollection access

    musicofnight

      Hi, I'm a beginner of Flex.

       

      I'm creating an app. It is on Rails framework. The front end is a Flex interface, while the back end is connected to a MySQL database. Now I'm having some trouble to access the data.

       

      To retrieve the data, I use RoR to find all the data from DB, and render it into a JSON structure. This step has no problem, as I can see the data structure is like this in my browser:

       

      [{"map": {"A": "aaa", "B": "bbb", "C": "ccc"}}, {"map": {"A": "aaa", "B": "bbb", "C": "ccc"}}]

       

      The next step is to decode JSON data into an ArrayCollection. In my .mxml file, I use a HTTPService to post all the JSON data. The code is this:

       

       

      <mx:HTTPService id="getData" url="/maps/create" method="POST" result="getAllData(event)" />

       

       

      In my .as file, the function is this:

       

       

      private function getAllData(event:ResultEvent):void

         {

           var rawData:String = new String(event.result);

           allDataArray = JSON.decode(rawData);

           allDataArrayCollection = new ArrayCollection(allDataArray);

         }

       

       

      Everything seems right till this step. When I use allDataArrayCollection.length, I can see there are 120 record. However, when I try to access the arrayCollection data, I have problem. For example, if I want to create another arrayCollection to contain only attribute A and B data inside allDataArrayCollection, I would do this:

       

      private function getNewData():void {

       

           for (var i:int = 0; i < allDataArrayCollection.length; i++) {

       

       

                newDataArrayCollection.addItem({A:allDataArrayCollection[i].map.A,

                B:allDataArrayCollection[i].map.B});

           }

       

      }

       

      However, it doesn't work. The newDataArrayCollection is empty. What makes it confusing is that, when I tried to access a specific item in .mxml file by using this:

       

      allDataArrayCollection.getItemAt(1).map.A

       

      It worked.

       

      I have the feeling that this is a small problem, but I am really stuck with it. Could someone please help me? I'd truly appreciate. Thanks!