JSON support in your Flex application

Posted on June 12, 2010
by

I don’t know why JSON is not natively supported in Flex, since it’s a widely used serialization format (even the latest version, Flex 4 when I write these lines). Whatever, if you want JSON serialization functions in your Flex application, you will need to add a library to your project by yourself.

However, Adobe itself made a JSON library that you can find at http://code.google.com/p/as3corelib/. Follow these steps to add your JSON library to your Flex project:

  1. Download the .zip archive and extract it. What you need is the .swc file in the lib folder.
  2. In Flex Builder / Flash Builder, go to your project’s properties, and then to the Flex Build Path panel.
  3. Click on Add SWC, and select the as3corelib.swc file
  4. In your project, add the following line: import com.adobe.serialization.json.JSON;

You should be able now to use JSON functionalities in your Flex project 🙂

About the author

Cyril Mazur is a serial web entrepreneur with experience in various fields: online dating, forex & finance, blogging, online advertising... who enjoys building things that people like to use.

16 comments

  1. Mike
    on January 1, 2011
    Thank You!
  2. Matthew
    on January 18, 2011
    That set of JSON classes are embarrassing -- date objects aren't supported and only the most rudimentary JSON structures are processed. They should derive off of JSON.net for more robust options-based serialization/deserialization.
  3. Joseph
    on May 27, 2011
    Precisely, I do have rather of a complex nested Objects with Arrays of Objects... Not sure how to solve this.
  4. mnjrupp
    on October 6, 2011
    I tried using the library in my Flex 4 application. No matter what I tried I would receive an error that the com.adobe.serialization.json.JSONDecoder not found. Even adding the global library path in Flashdevelop IDE would not work.Nor would placing a copy of the swc file in the project src directory work. I had to revert back to a flex 3 project. Then, and only then did it work.
  5. Cyril
    on October 6, 2011
    mnjrupp, have you well been to the project's properties (right click on project, then click properties) and added the .swc file?
  6. Gorge
    on December 2, 2011
    I have the same problem as mnjrupp. I correctly added the library as3corelib through project properties. I even have code completion working, but still the compile error "Access of undefined property JSON" remains. Did anyone solve this ?
  7. Gorge
    on December 6, 2011
    well, my problem was, that Flex SDK 4.6 has new internal variable called JSON, which was source of my ambiguous naming. Now I just specify the full name for JSON like model.json.JSON and it's ok ;-)
  8. raja
    on December 13, 2011
    Hi, i have successfully installed FB 4.6 and I’m getting compilation errors ---
    Can not resolve a multiname reference unambiguously. JSON (from /Applications/Adobe Flash Builder 4.6/sdks/4.6.0/frameworks/libs/air/airglobal.swc(JSON, Walker)) and com.adobe.serialization.json:JSON don’t know how to solve this. Can any one help me?

    Thanks,
    Raja.
  9. Gorge
    on December 14, 2011
    Hi raja,
    specify the full name of JSON object, when you use it, for example:
    com.adobe.serialization.json.JSON.decode(ultraJsonString);
  10. raja
    on December 14, 2011
    Hi Gorge:
    First of all many many thanks for the reply. I tried giving full name (com.adobe.serialization.json.JSON.decode(String), But it is raising another error—
    1120: Access of undefined property com. Any idea on this one please….
    Thanks,
    Raja.
  11. Gorge
    on December 17, 2011
    If you added swc file througn Properties->Library path->Add SWC Folder, then it should work...at least it does for me with this peace of code:

    import com.adobe.serialization.json.JSON;
    protected function init():void
    {
    var o:Object = {a:1,b:"monkey"};
    trace(com.adobe.serialization.json.JSON.encode(o));
    }

    Or in Flex 4.6 you can use the default JSON, don't import anything, just use:

    protected function init():void
    {
    var o:Object = {a:1,b:"monkey"};
    var s:String = JSON.stringify(o);
    trace(s);
    var o2:Object = JSON.parse(s);
    trace(ObjectUtil.toString(o2));
    }

    Hope, it helps...
  12. klub
    on September 10, 2012
    Thanks,
    The code moved to github
    github.com/mikechambers/as3corelib
  13. Cory
    on January 1, 2013
    I am using Flash Builder 4.7 and using a project that was originally created in Flex Builder 3.0. I keep getting the Ambiguous reference to JSON error. I have tried all the steps above with no luck. I noticed a lot of those fixes are over a year old. Does anyone have any updated steps for fixing this?

    Thank you.
  14. Cyril
    on January 1, 2013
    Oops sorry Cory, I haven't done any Flex recently to be honest, I personally gave up this technology for more HTML5 / Javascript, is anybody else could help I will tell you
  15. Hans
    on October 10, 2013
    @Cory,

    Had do digg up some old code, and had the same issue. I solved as the following:

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

    becomes

    var data:Object = com.adobe.serialization.json.JSON.decode(json_file);

    this removes the ambigious error for Flash Builder
  16. jsonHelper
    on November 16, 2013
    Nice Article..

    to test or validate json data you can use http://codebeautify.org/jsonviewer

Leave a Reply