利用JavaScript 實作JSON ( 字串轉物件、物件轉字串)
function Json_onclick() {
// A. 物件陣列,用中括號框住一組以 JSON 格式表現的物件
var objArray = [
{"firstName":"Derek", "lastName":"Jeter"},
{"firstName":"Jeremy", "lastName":"Lin"},
{"firstName":"Lionel", "lastName":"Messi"}
];
//JSON.stringify 將物件轉成Json字串
debug.innerText = JSON.stringify(objArray);
//JSON.parse 將Json字串 轉換成物件
var objJsonArray =JSON.parse(JSON.stringify(objArray));
debug.innerText="";
objJsonArray.forEach(function(obj){
debug.innerText+=obj.firstName+"-"+obj.lastName;
debug.innerHTML+="<br/>";
})
}