6 Replies Latest reply on Oct 14, 2011 8:15 AM by clb2196@gmail.com

    Help with JSON Decoding

    clb2196@gmail.com

      Hi guys, I'm kinda a n00b to Flex, and definitely a n00b to JSON decoding, hoping you can help me out.

       

      I just put the very basics in this code, I want to  be able to decode the JSON from the Rotten Tomatoes link, and display it in a datagrid. I seem to be only getting the top level (I took out the datagrid colums so you can see what it populates with). I want to get the actual movie info, which is one level further down. How do I do that?

       

      Here's the link I'm trying to read in: http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=u9pv54jkhnhemfx8x6u8q83r& q=Jack&page_limit=1

       

      And here's my full code:

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

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute"

                                              creationComplete="service.send()">

       

                <mx:Script>

                          <![CDATA[

                                    import mx.collections.ArrayCollection;

                                    import mx.rpc.events.ResultEvent;

                                    import com.adobe.serialization.json.JSON;

                                    import mx.utils.*;

       

                                    private function onJSONLoad(event:ResultEvent):void

                                    {

       

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

                                              var info:Object = JSON.decode(rawData);

                                              var dp:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(info));

                                              grid.dataProvider = dp;

       

                                    }

                                    var urlString:String = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=u9pv54jkhnhemfx8x6u8q83r& q=Jack&page_limit=1";

                          ]]>

                </mx:Script>

       

                <mx:HTTPService id="service" resultFormat="text"

                                                        url= "{urlString}"

                                                        result="onJSONLoad(event)" />

       

                <mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10">

                </mx:DataGrid>

      </mx:Application>

        • 1. Re: Help with JSON Decoding
          Raghuvasa Level 1

          Hi,

           

          I have modified Your code and placed it here. it is working fine.

           

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

           

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute"

           

                                                  creationComplete="service.send()">

           

           

           

                    <mx:Script>

           

                              <![CDATA[

           

                                        import mx.collections.ArrayCollection;

           

                      import mx.rpc.events.ResultEvent;

           

                      import com.adobe.serialization.json.JSON;

                      import com.adobe.serialization.json.JSONDecoder;

                      import com.adobe.serialization.json.JSONTokenizer;

           

                      import mx.utils.*;

                      import mx.controls.Alert;

           

                      private function onJSONLoad(event:ResultEvent):void

                      {

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

                          var info:Object = new JSONDecoder(rawData,true).getValue().movies as Object;

           

                          var dp:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(info));

                          grid.dataProvider = dp;

                      }

                      var urlString:String = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=u9pv5                 4jkhnhemfx8x6u8q83r&q=Jack&page_limit=1";

                  ]]

           

                              ]]>

           

                    </mx:Script>

           

           

           

                    <mx:HTTPService id="service" resultFormat="text"

           

                                                            url= "{urlString}"

           

                                                            result="onJSONLoad(event)" />

           

           

           

                    <mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10" >

                  <mx:columns>

                      <mx:DataGridColumn headerText="Title" dataField="title" />

                      <mx:DataGridColumn headerText="Year" dataField="year" />

                  </mx:columns>

           

              </mx:DataGrid>

           

          </mx:Application>

           

          And place column name which columns do want to show. And as3corelib.swc it is a library download it for jsondecoder.

          I think it would be helpful for you. 

           

          Thanks

          Raghu.

          • 2. Re: Help with JSON Decoding
            clb2196@gmail.com Level 1

            Hi Raghu,

             

            I tried that actually, but I copied your code in exactly, and it won't compile. It says "access of undefined property movies".

            • 3. Re: Help with JSON Decoding
              clb2196@gmail.com Level 1

              Anyone else have any ideas?

              • 4. Re: Help with JSON Decoding
                Flex harUI Adobe Employee

                Run one of the objects through ObjectUtil.toString() and post it here and tell us which fields you want to show.

                • 5. Re: Help with JSON Decoding
                  clb2196@gmail.com Level 1

                  I guess I'm not even understanding how the JSON object gets added to the array. How do I retrieve an individual object?

                  • 6. Re: Help with JSON Decoding
                    clb2196@gmail.com Level 1

                    Hm... strange. I re-copied your code, and although I had to change var info:Object = new JSONDecoder(rawData,true) to var info:Object = new JSONDecoder(rawData) it seems to work now. I'm not sure what I did before... but thanks!