4 Replies Latest reply on Sep 5, 2008 6:46 AM by rez82

    HELP Manipulating JSON arrays in FLEX

    rez82
      Hello.

      I’m trying to use JSON data in a Flex project, and am currently stuck. Here is the JSON example data:

      [{
      “tracklisting”:
      [{”artist”:”a”,”title”:”1”},
      {”artist”:”b”,”title”:”3”},
      {”artist”:”c”,”title”:”3”},
      {”artist”:”d”,”title”:”4”}]
      }]

      And I’m using corelib, but don’t know how the syntax to access the artist values within the tracklisting array. At the moment I’m trying (but getting no result):

      private function getArtistName (item:Object):String
      {
      return item.tracklisting.artist;
      }

      Any suggestions please (I'm new to AS3)?

      cheers,

      rez
        • 1. Re: HELP Manipulating JSON arrays in FLEX
          SujitG Level 2
          Hi,

          JSON.decode() function will return Array with objects in it. As your data has tracklisting object and that in turn has many child objects. So you will getting a Array with an object in its first index and there will be an Array named tracklisting with other child objects as property of the object. You can try adding a break point in your code and see how the object returned by JSON.decode structure is.

          I got the sample working. Please find the attached code.

          Hope this helps.
          • 2. Re: HELP Manipulating JSON arrays in FLEX
            rez82 Level 1
            Thanks for your reply Sujit.

            I've got the code working as well now, although it's only returning the value for the first artist object in the tracklisting array. How could I return all the artist values??

            thanks,

            rez
            • 3. Re: HELP Manipulating JSON arrays in FLEX
              SujitG Level 2
              Hi Rez,

              tracklisting is an Array with objects in it. If you want all the artist names, you can just loop through the tracklisting Array and get the data from each object in the tracklisting Array.

              Hope this helps.
              • 4. Re: HELP Manipulating JSON arrays in FLEX
                rez82 Level 1
                Here's the code I tried to use, but it only gives me the artist value for the last object in the tracklisting array

                private function getArtistName( item:Object, column:DataGridColumn ):String
                {
                for each (var track:Object in item.tracklisting)
                {
                var artistvalue = track.artist;
                }
                return artistvalue;
                }

                I think that my artistvalue and return statements are wrong, what is the correct code for returning all the artist values??
                Thanks again for your help Sujit,

                rez