ActionScript處理JSON數據~~

2009-05-09
要用ActionScript來處理JSON數據,首先下載這東東:
as3corelib

as3corelib包裡有一個JSON類,可以將一個AS Object轉換為JSON格式的字符串,例如:
var obj:Object = {
    username : "Oscar Tong",
    password : "123456",
    age : 23,
    male: true,
    contacts: {
        mobile: "13800138000",
        phone: "020-39322849",
        addr: "guangdong university of technology"
    }
}
  
var json:String = JSON.encode(obj);
trace(json);

輸出JSON格式的字符串為:
{"username":"Oscar Tong",
 "age":23,
 "password":"123456",
 "male":true,
 "contact":
    {"addr":"guangdong university of technology",
     "phone":"020-39322849",
     "mobile":"13800138000"}
}

貌似輸出結果的順序有點亂了,另外JSON還有一個靜態方法decode,可以將JSON格式的字符串轉換為一個AS Object,具體自己看看包裡附帶的reference吧~~

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com