AS3读取外部JSON数据并给文本框赋值

初次用到flash读取JSON,经过几天的研究,总算告一段落。现总结备忘,也供朋友们参考。

 

1       准备JSON数据

编写aspx代码,或ASP代码,在页面输出JSON数据,例如:

http://localhost:1771/outPutJSON.aspx 

输出内容为:

[{"name":"Hans","age":"32"},{"name":"John","age":"12"},{"name":"Zaki","age":"34"},{"name":"Dr. Cox","age":"88"}]

2       AS3环境部署

AS解析JSON需要用到以下库:

 

 

将以下库复制到fla同一目录下的flashLib目录中。

不知道如何上传附件,如需附件请联系QQ: 396068801,也可以从百度文档获得附件,网址:http://wenku.baidu.com/view/11682d4af7ec4afe04a1dfb6.html?st=1

 

3       启动Adobe Flash CS4 Professional,新建fla文件,保存文件名为readJson.fla

单击菜单栏右侧的下拉菜单,选择“动画”,在工作区中新建一动态文本框,名称为inforContent。

 

 

 

4       编写脚本

在时间轴第1帧加入Action: 单击时间轴第1帧以选中第1帧,再单击动作-帧标签,打开脚本编辑器

 

脚本如下:

import flashLib.JSON;

import flash.display.Sprite;

import flash.events.Event;

import flash.net.URLLoader;

import flash.net.URLRequest;

import flash.net.URLVariables;

 

//var newRequest:URLRequest = new URLRequest(MovieClip(parent).HOMEURL+"/outPutJSON.aspx");

var newRequest:URLRequest = new URLRequest("http://localhost:1771/ArcGisWeb/outPutJSON.aspx");

 

var variables:URLVariables = new URLVariables();

 

var loader:URLLoader = new URLLoader();

loader.addEventListener(Event.COMPLETE, decodeJSON);

 

 

function getJSON() {

         newRequest.method = "POST";

         newRequest.data = variables;

         loader.load(newRequest);

        

}

  //replaceStr

 

 var ntitle,ndesc

 var sl:Number=12

 var dl:Number=30

var infoList:Array=new Array();

function decodeJSON(evt:Event):void {

   var info = JSON.decode( URLLoader( evt.target ).data ); 

   var jsonArray:Array = info;

   for (var i=0; i<jsonArray.length; i++) {

            trace( jsonArray[i].name );

            //inforContent.text += jsonArray[i].name+'\n';

            inforContent.appendText(jsonArray[i].name+'\n');

   }

   trace( URLLoader( evt.target ).data );

}

//resetItem();

getJSON();

stop();

5       脚本说明

Import JSON; //JSON是外部库,需要下载JSON.as并将JSON.as复制到 当前的fla同一个目录下。

JSON.as内容:

 

package flashLib

{

         /**

          * This class provides encoding and decoding of the JSON format.

          *

          * Example usage:

          * <code>

          *             // create a JSON string from an internal object

          *             JSON.encode( myObject );

          *

          *              // read a JSON string into an internal object

          *              var myObject:Object = JSON.decode( jsonString );

          *     </code>

          */

         public class JSON {

        

        

                   /**

                    * Encodes a object into a JSON string.

                    *

                    * @param o The object to create a JSON string for

                    * @return the JSON string representing o

                    * @langversion ActionScript 3.0

                    * @playerversion Flash 9.0

                    * @tiptext

                    */

                   public static function encode( o:Object ):String {

                           

                            var encoder:JSONEncoder = new JSONEncoder( o );

                            return encoder.getString();

                  

                   }

                  

                   /**

                    * Decodes a JSON string into a native object.

                    *

                    * @param s The JSON string representing the object

                    * @return A native object as specified by s

                    * @throw JSONParseError

                    * @langversion ActionScript 3.0

                    * @playerversion Flash 9.0

                    * @tiptext

                    */

                   public static function decode( s:String ):* {

                           

                            var decoder:JSONDecoder = new JSONDecoder( s )

                            return decoder.getValue();

                           

                   }

        

         }

 

}

6       执行效果

 

 

posted @ 2012-06-01 11:26  guo2008  阅读(4091)  评论(0编辑  收藏  举报