JSON,MessagePack导出和读取

JSON,MessagePack导出和读取

已有 86 次阅读  2012-06-20 20:02   标签:  JSON  MessagePack 
public static function writeJSON(object:Object,url:String):void
  {
   var _urlStr:String;
   var _file:File = new File();
   _urlStr = File.applicationDirectory.nativePath;
   _urlStr = _urlStr.replace(/\\/g, "/");
   _file = _file.resolvePath(_urlStr + url);
   var _fileStream:FileStream = new FileStream();
   _fileStream.openAsync(_file, FileMode.WRITE);
   
   //导出MessagePack格式
   var byteArray:ByteArray = MessagePack.encode(object);
   _fileStream.writeBytes(byteArray,0,byteArray.length);
   
   //导出amf格式
   _fileStream.writeObject(object);
  }
 
//读取
private var loader:URLLoader = new URLLoader();
 private var url:String = MessagePack/amf

  public function test()
  {
   //二进制数据形式接收
   loader.dataFormat = URLLoaderDataFormat.BINARY;
   loader.load(new URLRequest(url));
   loader.addEventListener(Event.COMPLETE, onComplete);
  }
  public function onComplete(event:Event):void
  { 
   var byteArray:ByteArray = event.target.data;
   //解码读取
   trace(JSON.stringify(MessagePack.decode(byteArray)));
  }  
  public function onComplete2(event:Event):void
  { 
   var byteArray:ByteArray = event.target.data;
   var obj:Object = byteArray.readObject();
   trace(JSON.stringify(obj));
  }

你可能感兴趣的:(Flash,json,object,function,file,url,string)