Search code examples
xmljsonapache-flexflash-builder

parse Json to XML in Flex 4


I'm making a small project in flex(flash builder 4.5) where the user can search for books. At the moment I can get books (im using google books api) but I receive it as JSON format.

I would like to get this in xml instead so i can show a few fields in a datagrid.

So far I've downloaded the as3corelib and linked that to my project. But I cant figure out how to Decode the JSON. JSON example

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   creationComplete="application1_creationCompleteHandler(event)"
                   >
<fx:Script>
    <![CDATA[
        import com.adobe.serialization.json.JSON;

        import mx.controls.Text;
        import mx.events.FlexEvent;
        import mx.rpc.events.ResultEvent;
        [Bindable] private var api_request:String;

        import com.adobe.serialization.json.JSONDecoder;


        protected function application1_creationCompleteHandler(event:FlexEvent):void
        {
            // TODO Auto-generated method stub
            btnSearch.addEventListener(MouseEvent.CLICK, Zoek);
        }

        protected function Zoek(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            var api_url:String='https://www.googleapis.com/books/v1/volumes?q=';
            var api_tag:String=search.text;

            api_request= api_url + api_tag

            GoogleBooks.send(); 


        }

        protected function GoogleBooks_resultHandler(event:ResultEvent):void
        {

            txtResult.text=event.result as String;
            //JSONDecoder(txtResult.text);

        }

    ]]>
</fx:Script>
<fx:Declarations>
    <s:HTTPService id="GoogleBooks" url="{api_request}" resultFormat="text" result="GoogleBooks_resultHandler(event)" />


</fx:Declarations>
<s:Button x="175" y="40" label="Button" id="btnSearch"/>
<s:TextInput x="26" y="38" id="search"/>

<s:TextArea id="txtResult" x="32" y="112" width="539" height="312"/>
</s:WindowedApplication>

so my question is: How can i decode the JSON so i can see xml in my Textarea?


Solution

  • It depends on how you want to use the data. You could simply do

    var data:Object = JSON.decode(jsonString);
    

    or you could use the JSONDecoder class to do the same:

    var decoder:JSONDecoder = new JSONDecoder(jsonString, jsonStringMatchesStandard);
    var data:Object = decoder.getValue();
    

    or you could use the JSONDecoder to decode the string token by token:

    var decoder:JSONDecoder = new JSONDecoder(jsonString, jsonStringMatchesStandard);
    var token:JSONToken = decoder.nextToken();
    

    I would recommend looking in the comments of the sources the give good guidance on how to use the library.

    https://github.com/mikechambers/as3corelib/tree/master/src/com/adobe/serialization/json