2017年1月5日 星期四

最簡單的方法 1. 把物件轉成 JSON 字串 2. 把JSON 字串轉成 物件 (陣列物件版)

How to transfer single JSON string for objects and then restore them
GitHub: Source

JSON String Array

let strJsonSingleString = '[' +  obj.toJson() + ',' + obj2.toJson() + ']';

JSON String Array -> Object Array

var Objs = JSON.parse(strJsonSingleString);
Objs[i].__proto__ = MyClass.prototype


Example
class MyClass{
 constructor(strName, intAge) {
   this._strName = strName;
   this._intAge = intAge;
 }

 toJson(){
   return JSON.stringify({"_strName":this._strName, "_intAge":this._intAge});
 }

 MethodTest(strMessage){
   console.log("MethodTest::" + strMessage);
 }
}

function main() {
 // Create two objects
 let obj = new MyClass("Jing", "1234");   
 let obj2 = new MyClass("Tom", "57");

 // Show JSON string
console.log("Json" + obj.toJson());
console.log("Json" + obj2.toJson());

 // Generate one single JSON string for these objects
 // [JSON] Single String
 let strJsonSingleString = '[' +  obj.toJson() + ',' + obj2.toJson() + ']';
 console.log('strJsonSingleString = ' + strJsonSingleString);

 // Restore the objects and test
 var Objs = JSON.parse(strJsonSingleString);
 console.log('objs = ' , Objs);
 for (let i = 0; i < Objs.length; i++) {
   Objs[i].__proto__ = MyClass.prototype
   console.log('Objs._strName =' + Objs[i]._strName);
 }
}

main();


E.g.