topical media & game development
server-amf-json-service.mx
server-amf-json-service.mx
[swf]
flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="500" height="410"
>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import com.adobe.serialization.json.JSON;
person
private function personJSON(event:ResultEvent):void
{
//get the raw JSON data and cast to String
var rawData:String = String(event.result);
var person:Object = JSON.decode(rawData);
txtName.text = person.first_name + " "
+ person.last_name;
txtEmail.text = person.email;
txtAddress.text = person.address;
txtEmployees.text = "No";
txtTitle.text = "No Title";
//Data Grid Code
dgEmployees.dataProvider = null;
}
manager
private function managerJSON(event:ResultEvent):void
{
//get the raw JSON data and cast to String
var rawData:String = String(event.result);
var manager:Object = JSON.decode(rawData);
txtName.text = manager.first_name + " "
+ manager.last_name;
txtEmail.text = manager.email;
txtAddress.text = manager.address;
txtEmployees.text = "Yes";
txtTitle.text = manager.title;
//Data Grid Code
var employees:Array =
manager.employees as Array;
var employeesCollection:ArrayCollection =
new ArrayCollection(employees);
dgEmployees.dataProvider = employeesCollection;
}
]]>
</mx:Script>
request person
<mx:HTTPService id="personRequest"
url=<server-amf-json-service.php>
useProxy="false" method="GET" resultFormat="text"
result="personJSON(event)">
<mx:request xmlns="">
<getPerson>true</getPerson>
</mx:request>
</mx:HTTPService>
request manager
<mx:HTTPService id="managerRequest"
url=<server-amf-json-service.php>
useProxy="false" method="GET" resultFormat="text"
result="managerJSON(event)">
<mx:request xmlns="">
<getManager>true</getManager>
</mx:request>
</mx:HTTPService>
layout
<mx:Panel x="0" y="0" width="500" height="410"
layout="absolute" title="Simple JSON Example">
<mx:DataGrid x="10" y="174" width="460" enabled="true"
editable="false" id="dgEmployees" height="156">
<mx:columns>
<mx:DataGridColumn headerText="First Name"
dataField="first_name"/>
<mx:DataGridColumn headerText="Last Name"
dataField="last_name"/>
<mx:DataGridColumn headerText="Email"
dataField="email"/>
<mx:DataGridColumn headerText="Address"
dataField="address"/>
</mx:columns>
</mx:DataGrid>
<mx:Button x="116" y="338" label="Get Employee"
id="getPerson" click="personRequest.send();"/>
<mx:Button x="266" y="338" label="Get Manager"
id="getManager" click="managerRequest.send();"/>
<mx:Label x="131" y="12" text="Name"/>
<mx:TextInput x="189" y="10" id="txtName" editable="false"/>
<mx:Label x="131" y="42" text="E-mail"/>
<mx:TextInput x="189" y="40" id="txtEmail" editable="false"/>
<mx:Label x="131" y="68" text="Address"/>
<mx:TextInput x="189" y="66" id="txtAddress"
editable="false"/>
<mx:Label x="131" y="94" text="Title"/>
<mx:TextInput x="189" y="92" id="txtTitle" editable="false"/>
<mx:Label x="131" y="122" text="Has Employees"/>
<mx:TextInput x="229" y="120" width="120" editable="false"
id="txtEmployees" text="No"/>
<mx:Label x="10" y="148" text="Employees:"/>
</mx:Panel>
</mx:Application>
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.