หน้าเว็บ

วันศุกร์ที่ 26 ตุลาคม พ.ศ. 2555

convert javascript object to json


/*
*Talker class
*/
function Talker(){
    this.getId = function(){
        return this.id;
    }
    this.setId = function(id){
        this.id = id;
        return this;
    }
    this.getChannel = function(){
        return this.channel;
    }
    this.setChannel = function(channel){
        this.channel = channel;
        return this;
    }
    this.getName = function(){
        return this.name;
    }
    this.setName = function(name){
        this.name = name;
        return this;
    }
    this.getPicture = function(){
        return this.picture;
    }
    this.setPicture = function(picture){
        this.picture = picture;
        return this;
    }
    this.getLink = function(){
        return this.link;
    }
    this.setLink = function(link){
        this.link = link;
        return this;
    }
    
}
/*
*end Talker class
*/
/*
/*TalkData class
*/
function TalkData(){
    this.getTalker = function(){
        return this.talker;
    }
    this.setTalker = function(talker){
        this.talker = talker;
        return this;
    }
    this.getTime = function(){
        return this.time;
    }
    this.setTime = function(time){
        this.time = time;
        return this;
    }
    this.getMessage = function(){
        return this.message;
    }
    this.setMessage = function(message){
        this.message = message;
        return this;
    }
    this.getTalkWiths = function(){
        if(this.talkWiths == null){
            this.talkWiths = new Array();
        }
        return this.talkWiths;
    }
    this.setTalkWiths = function(talkWiths){
        this.talkWiths = talkWiths;
        return this;
    }
    
} 
/*
*end TalkData class
*/


/*
*Test code
*/
var talker = new Talker();
talker.setId("1");
talker.setChannel("11111111");
talker.setName("talker1");
talker.setPicture("picture1");
talker.setLink("link1");

var talkWith1 = new Talker();
talkWith1.setId("2");
talkWith1.setChannel("22222222");
talkWith1.setName("talker2");
talkWith1.setPicture("picture2");
talkWith1.setLink("link2");

var talkWith2 = new Talker();
talkWith2.setId("3");
talkWith2.setChannel("33333333");
talkWith2.setName("talker3");
talkWith2.setPicture("picture3");
talkWith2.setLink("link3");


var talkWiths = new Array();
talkWiths.push(talkWith1);
talkWiths.push(talkWith2);

var data = new TalkData();
data.setTalker(talker);
data.setTime(999);
data.setMessage("hello world");
data.setTalkWiths(talkWiths);

alert(JSON.stringify(data));
/*
*end Test code
*/

ไม่มีความคิดเห็น:

แสดงความคิดเห็น