Story Pixel

Independent Interaction of Sam Wilson

  • ketan patel

    hi this is very nice

    Fri, July 10, 2009 at 2:30am
    ketan patel's gravatar
  • f


    Thu, July 23, 2009 at 12:27pm
    f's gravatar

have your say...

Loading JSON data

A very useful method of transferring data between Flash and a data source is JSON. Here is some code from a recent project that loads JSON in a usable form.

So let’s say you have a JSON file you’d like to load into Flash as a data source, something like this maybe:

{ "sitedata":{ "title":"School is in", "assets":{ "splashimage":{ "source":"images/interface/splash.png" }   },   “sections”:{ "section":[ { "source":"swfs/slides/demo.swf", "title":"READING" },   { "source":"images/slides/write.jpg", "title":"WRITING" },   { "source":"images/slides/numbers.jpg", "title":"MATH" },   { "source":"images/slides/food.jpg", "title":"LUNCH" }   ]   } } }

In order to get to the data, we have to read in this data, and assign it to a variable. I used BulkLoader for to load in the JSON file here.

import; import com.adobe.serialization.json.JSON; import com.stimuli.loading.BulkLoader; var dataloader:BulkLoader; var jsondata:Object; /* * Load the json data */ public function load() : void { dataloader = BulkLoader.createUniqueNamedLoader(); dataloader.logLevel = BulkLoader.LOG_ERRORS; dataloader.add("somedata.json", {priority:30, id:"config-json"}); dataloader.addEventListener(BulkLoader.COMPLETE, onConfigLoadComplete); dataloader.start(); }; /* * When the json has loaded, assign it to a variable. */ function onConfigLoadComplete(event : Event) : void { dataloader.removeEventListener(BulkLoader.COMPLETE, onConfigLoadComplete); // 'sitedata' will be whatever you decide ... see 'sitedata' in my json file above jsondata = JSON.decode(dataloader.getText("config-json")).sitedata; trace (jsondata.title) // "School is in" }

So that’s it. Although I’ve given this example not in a class, it was written originally inside a class. If there are issues leave me a message, I will compile the code to verify it’s without snafus.