Flex handles return data in Json format

Source: Internet
Author: User

Pre-order:

First, you know what JSON is, what the JSON format looks like, and secondly, the need to understand JSON processing is that servlet communication from Flex and Java cannot be passed through objects, so JSON is required. Finally, note that the JSON processing of flex JSON and Java requires the introduction of different packages:

Required in Flex: AS3CORELIB.SWC

The add location of the package, under the Flex directory, or under the Ecplilse project path (but this is not certain to be placed here)

Required in Java: Json-lib-2.2.2-jdk15.jar and Jsoup-1.6.3.jar

My own placement D:\SOFTWARE\ZN, the purpose is for us to find, and you have no matter

Case:

Flex's main code:

<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml"  layout= "vertical"  creationcomplete = "init ()"  ><mx:script><! [Cdata[    import com.adobe.serialization.json.json;private function init () :void{    var userid:int = 2;        Requestsend (userid);    } private function requestsend (Userid:int):void{       http_service.addeventlistener (Resultevent.result,http_service_resulthandler);       http_service.addeventlistener (Faultevent.fault,http_service_faulthandler);       http_service.url= "http://localhost:8080/FlexProjectInformation/servlet/ getinformation?userid= "+userid;      http_service.send (null);  }   protected function http_service_resulthandler (event:resultevent):void{      /**  processing for array  resultformat= "Object"  ************************ /    var objectstr:string=event.result as string;       var jsonobj:object =com.adobe.serialization.json.json.decode (OBJECTSTR);// The aim is to assemble json    var arr:array =jsonobj as array;      for (var i=0 ;i<arr.length;i++) {    //handles what is placed in json }/**  Processing for array  resultformat= "object"  ***********************************//**  processing for ArrayCollection   resultformat= "Object"  ***********************************/var objectstr:string= event.result as string;var arr:array =  (Json.decode (OBJECTSTR)  as Array); Arrac =new arraycollection (arr); for (var i:int=0;i<arrac.length;i++) {       var o:object&nbsP;= arrac.getitemat (i);       alert.show (o["projecttable"],o["projectname"], o["dnt"]);  } /**  processed as arraycollection  resultformat= "Object"  *************** ** arraycollection  Display ***********************************/<mx:datagrid  Id= "DG"  dataprovider= "{Arrac}" >    <mx:columns>         <mx:datagridcolumn headertext= "ID"  datafield= "projecttable"/>         <mx:datagridcolumn headertext= "Name"  dataField= " ProjectName "/>        <mx:datagridcolumn headertext=" age "  datafield= "CSQ"/>    </mx:columns></mx:datagrid>/**  arraycollection  Display ***********************************/}&NBSP;&NBSP;PROTECTED&NBSP;FUNCTION&NBSP;HTTP_ Service_faulthandler (event:faultevent): VOID{&NBsp;     alert.show ("no Data");}   ]]></mx:script><mx:httpservice id= "Http_service"  url= ""  method= "GET"  resultformat= "Object" ></mx:HTTPService></mx:Application>

java servlet main code:

String userid = new string (Request.getparameter ("userid"). GetBytes ("Iso-8859-1"), "Utf-8"); Response.setheader ("Content-type",  "Text/html;charset=utf8"); Response.setcharacterencoding ("UTF8");   printwriter out = response.getwriter ();//sonprojectarraylist  is a arraylisttry {     jsonarray jsonarray = jsonarray.fromobject (sonprojectArrayList);     /*int jsonarraysize =jsonarray.size ();    for  (int j  = 0; j < jsonarraysize; j++)  {         jsonobject json = jsonobject.fromobject (Jsonarray.get (j));         //out.print ("json: " +json+ "\ n");         Out.print (json+ ",");//Output Data format  {}    }*/    out.print (jsonArray);// Output data format [{},{},{},{},{},{}]} catch  (exception e)  {    system.out.println ("JSON&NBSP;:" + E.getmessage ());} Out.flush ();   out.close ();

PS: The most important thing in this process is

resultformat= "Object" Resultformat the return value of the setting, the default is object, of course, you can specify the format of the input array XML and so on. in other words, the data organization format of this end of Java affects the efficiency and convenience of flex-side data processing, and the best way is to "unify" the data processing on both ends, or

/** can generate ArrayCollection, arraycollection is placed in Arrayvar objectstr:string=event.result as String;var Arr:Array = ( Json.decode (OBJECTSTR) as Array); var arrac:arraycollection =new arraycollection (arr); */

When Java and database are connected to retrieve relevant information, the information needed in the database is taken out at once into memory for processing map, if memory processing is allowed.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.