13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Using native JSON functionality<br />

}<br />

}<br />

}<br />

}<br />

cont<strong>en</strong>ts[a.id] = this[a];<br />

// We also wrap the cont<strong>en</strong>ts in an object so that we can<br />

// id<strong>en</strong>tify it by looking for the marking property "class E"<br />

// while in the midst of JSON.parse.<br />

return {"class JSONDictionaryExtnExample": cont<strong>en</strong>ts};<br />

// This is just here for debugging and for illustration<br />

public function toString():String {<br />

var retval = "[JSONDictionaryExtnExample ]"<br />

return retval;<br />

}<br />

Wh<strong>en</strong> the following runtime script calls JSON.parse() on a JSONDictionaryExtnExample object, the reviver<br />

function calls JSONG<strong>en</strong>ericDictExample.revive() on each object in JSONDictionaryExtnExample. This call<br />

extracts the ID that repres<strong>en</strong>ts the object key. The JSONG<strong>en</strong>ericDictExample.revive()function uses this ID to<br />

retrieve and return the stored JSONDictionaryExtnExample object from a private static array.<br />

import flash.display.MovieClip;<br />

import flash.text.TextField;<br />

var a_bob1:JSONG<strong>en</strong>ericDictExample = new JSONG<strong>en</strong>ericDictExample("Bob", new<br />

Date(Date.parse("01/02/1934")));<br />

var a_bob2:JSONG<strong>en</strong>ericDictExample = new JSONG<strong>en</strong>ericDictExample("Bob", new<br />

Date(Date.parse("05/06/1978")));<br />

var a_j<strong>en</strong>:JSONG<strong>en</strong>ericDictExample = new JSONG<strong>en</strong>ericDictExample("J<strong>en</strong>", new<br />

Date(Date.parse("09/09/1999")));<br />

var e = new JSONDictionaryExtnExample();<br />

e[a_bob1] = {earnings: 40, violations: 2};<br />

e[a_bob2] = {earnings: 10, violations: 1};<br />

e[a_j<strong>en</strong>] = {earnings: 25, violations: 3};<br />

trace("JSON.stringify(e): " + JSON.stringify(e)); // {"class JSONDictionaryExtnExample":<br />

//{"id_class_JSONG<strong>en</strong>ericDictExample_10001":<br />

//{"earnings":10,"violations":1},<br />

//"id_class_JSONG<strong>en</strong>ericDictExample_10002":<br />

//{"earnings":25,"violations":3},<br />

//"id_class_JSONG<strong>en</strong>ericDictExample_10000":<br />

// {"earnings":40,"violations":2}}}<br />

var e_result = JSON.stringify(e);<br />

Last updated 6/6/2012<br />

122

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!