AS3使用Json 传复杂数据 -------- 用数组而不是向量
package { public class Person { private var name:String; public function get Name():String { return name; } private var age:int; public function get Age():int { return age; } private var job:String; public function get Job():String { return job; } public function Person(_name:String,_age:int,_job:String) { name = _name; age = _age; job = _job; } } }
package { public class Company { public function get Name():String { return name; } private var name:String; public function get EstablishmentDate():int { return establishmentDate; } private var establishmentDate:int; public function get EmployeeNum():int { return employees.length; } private var employeeNum:int; public function get Employees():Array{ return employees; } private var employees:Array; public function Company(_name:String,_establishment:int) { name = _name; establishmentDate = _establishment; employees = new Array(); } public function AddEmployee(_name:String, _age:int, _job:String):Person { var p:Person = new Person(_name, _age, _job); employees.push(p); return p; } } }
准备传一个公司的数据,公司里有雇员。
var company:Company = new Company("HelloCompany", 2013); company.AddEmployee("John", 22, "AJob"); company.AddEmployee("Bob", 32, "BJob"); company.AddEmployee("Mark", 20, "ADJob"); company.AddEmployee("May", 42, "ACJob"); var c:Object = JSON.encode(company);
注意一点,如果雇员用向量存储,就会出错。