The option to use JSON or XML with ActionScript is available. In order to use JSON you will need to include the Adobe corelib library. This library contains functionality to encode and decode JSON streams. In the example below, the pEvent object is of type Event and is supplied by the Flash runtime when the handler method is called. pEvent.target.data contains the response data stream. This stream is decoded into an Object. The sample iterates through the properties constructing an object for each name/value pair and then adds it to an ArrayCollection object.

var json:Object = JSON.decode(pEvent.target.data);
for (var propName:String in json)
 arrayCollection.addItem({name: propName, value: json[propName]});

The following sample uses XML input. Note that you could also take advantage of the ECMAScript for XML functionality once the XML object is created. The following sample does not use that approach, though it is an option.

var xml:XML = new XML(pEvent.target.data);
var xmlList:XMLList = pXML.elements();
if (xmlList.length() == 0)
 xmlList = new XMLList(pXML);
 var count:int = 0;
for each(var item:XML in xmlList) {
 if (item.name() != "element")
 array.addItem({name: item.name(), value: item.text()});
 else if (item.hasComplexContent()) {
 var elementList:XMLList = item.child("element");
 if (elementList != null && elementList.length() > 0) {
 var count2:int = 0;
 for each (var el:XML in elementList)
 array.addItem({name: count2++, value: el.text()});
 }
 else
 array.addItem({name: item.key.text(), value: item.value.text()});
 }
 else
 array.addItem({name: count++, value: item.text()});
}

Copyright © 1997, 2015 Oracle and/or its affiliates. All rights reserved. Legal Notices