JSON.stringify(), JSON資料轉字串

JSON.stringify(), JSON資料轉字串

//JSON資料
var data = { 
  "name": "jsbin",
  "age": 99,
  "birthday": "1988/02/02",
  "mails": {"home": "hotmail@mail.com", "work": "yahoo@mail.com"},
  "phones": {"mobile": "0923456789", "home": "048353868" }
}

//轉字串
var json_str = JSON.stringify(data);
alert(json_str);

JSON.parse(), 將字串JSON物件

//JSON string 
var json_str = '{"name":"jsbin","age":99,"birthday":"1988/02/02"';
json_str.concat(',"mails":{"home":"hotmail@mail.com","work":"yahoo@mail.com"}');
json_str.concat(',"phones":{"mobile":"0923456789","home":"048353868"}}');

//轉JSON物件
var json = JSON.parse(json_str);
alert(json.mails.home);

收工

沒有留言: