3 Replies Latest reply on Jun 15, 2010 7:42 AM by jp77

    Urgent JSON container to as3 object.

    Antony52

      I have a JSON that comes as a container in the format {id:"", name:"", values:[]} etc. After using Json decode in what as3 type do I cast it and how do I access the various values? When I cast it to object I cannot seem to find out how to access the various fields. Thanks you.

        • 1. Re: Urgent JSON container to as3 object.
          jp77 Level 1

          I have this JSON of string value pairs:

           

          var searchJSON:String = {"results":[{"name":"jack","surname":"four","avatar":"vpac://jack_Icon_.dat"},{"name":"je nny","surname":"three","avatar":"vpac://jenny_Icon_.dat"},{"name":"charles","surname":"two ","avatar":"vpac://charles_Icon_.dat"}]}

           

          To parse into an object or array whatever you prefer use this structure:

           

          var obj:Object = JSON.decode(searchJSON) as Object;
          trace(obj.results.length);

           

          Then depending on your object structure:

           

          for (var i:int; i<obj.results.length; i++)
          {

               trace(obj.results[i].name);

               trace(obj.results[i].avatar);

          }

           

          myList.dataProvider = new ArrayCollection(obj.results);

           

          etc ...

           

          I assume you have as3corelib.swc in your project libs folder, as this is where JSON.decode will be created from.

           

          Hope that helps!

           

           

           

          • 2. Re: Urgent JSON container to as3 object.
            Antony52 Level 1

            Thank you for the reply. I seem to get :"A term is undefined and has no properties" when in the line: var obj:Object =  JSON.decode(searchJSON) as Object; The json comes fine though?

            • 3. Re: Urgent JSON container to as3 object.
              jp77 Level 1

              Sorry made a mistake when cleaning out real project data in JSON String this is better: In full code this is how it looks:

               

              <?xml version="1.0" encoding="utf-8"?>

              <mx:Application

               

               

              xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="doJSON()">

               

              <mx:Script>

              <![CDATA[

               

              import mx.collections.ArrayCollection;

               

              import com.adobe.serialization.json.JSON;

               

               

              private function doJSON():void

              {

               

              var searchJSON:String = '{"results":[{"name":"jack","surname":"four","avatar":"vpac://jack_Icon_.dat"},{ "name":"jenny","surname":"three","avatar":"vpac://jenny_Icon_.dat"},{"name":"cha rles","surname":"two","avatar":"vpac://charles_Icon_.dat"}]}';

               

              var obj:Object = JSON.decode(searchJSON) as Object;

               

              trace(obj.results.length);

               

               

              for (var i:int; i<obj.results.length; i++)

              {

               

               

              trace(obj.results[i].name);

               

              trace(obj.results[i].avatar);

               

              }

               

               

              myList.dataProvider =

              new ArrayCollection(obj.results);

              myList.labelField =

              'avatar';

              }

               

              ]]>

               

              </mx:Script>

               

              <mx:List id="myList" />

              </mx:Application>