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       執行效果

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM