由于一直用json开发,对json比较熟悉了,所以学习flex以后一直想做一个flex与json的开发,网上海一样的资源一直没找到合适的,今天偶然间搜到一篇文章才知道flex早就支持json解析。这里提供资源下载网站
http://code.google.com/p/as3corelib/downloads/list 。顺便将例子上传上来希望对大家学习有帮助。
举例如下:
步骤1:到上面的链接中下载swc文件到lib包下
步骤2:在webRoot下新建文件夹flexds,其下新建文件json.jsp
http://code.google.com/p/as3corelib/downloads/list 。顺便将例子上传上来希望对大家学习有帮助。
举例如下:
步骤1:到上面的链接中下载swc文件到lib包下
步骤2:在webRoot下新建文件夹flexds,其下新建文件json.jsp
- json.jsp内容如下:
- <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
- <jsp:directive.page import="java.text.*"/>
- <jsp:directive.page import="java.lang.*"/>
- <jsp:directive.page contentType="text/json"/>
- [<jsp:scriptlet>
- <![CDATA[
- double compa = 1000.0;
- double compb = 900.0;
- for (int i = 0; i<=30; i++) {
- compa += ( Math.random() * 100 ) - 50;
- compb += ( Math.random() * 100 ) - 50;
- if ( i > 0 ) out.print( "," );
- ]]> </jsp:scriptlet>
- {"compa":<jsp:expression>compa</jsp:expression>,
- "compb":<jsp:expression>compb</jsp:expression>}<jsp:scriptlet>
- <![CDATA[ }
- ]]>
- </jsp:scriptlet>
- ]
- </jsp:root>
- 步骤3:下面做测试文件flexSrc下新建jsondg.mxml文件,内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- creationComplete="jsonservice.send()">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import com.adobe.serialization.json.JSONDecoder;
- import mx.controls.Alert;
- private function onJSONResult(event:ResultEvent):void
- {
- var data:String=event.result.toString();
- data=data.replace(/\s/g, '');
- var jd:JSONDecoder=new JSONDecoder(data);
- dg.dataProvider=jd.getValue();
- }
- ]]>
- </mx:Script>
- <mx:HTTPService id="jsonservice"
- url="http://localhost:8080/felxDOC/flexds/json.jsp"
- resultFormat="text"
- result="onJSONResult(event)"/>
- <mx:Panel title="Stock Data "
- width="100% "
- height="100% ">
- <mx:DataGrid id="dg"
- width="100%"
- height="100%">
- <mx:columns>
- <mx:DataGridColumn dataField="compa"/>
- <mx:DataGridColumn dataField="compb"/>
- </mx:columns>
- </mx:DataGrid>
- </mx:Panel>
- </mx:Application>