/* 字典目录 数据存储 */ var editData = { }; editData.data = {}; editData.Add = function (key, obj) { editData.data[key] = obj }; editData.GetData = function () { var data = editData.data; for (var key in editData.data) alert(key+":"+data[key]); }; editData.Delete = function (key) { delete editData.data[key]; }; editData.Add("a1", 1); editData.Add("a2", 2); editData.Add("a3", 3); editData.Add("a4", 4); editData.GetData(); editData.Delete("a3"); editData.GetData();
定义对象 输出JSON对象数据不同状态
/* JSON.stringify: <script type="text/javascript" src="https://raw.github.com/douglascrockford/JSON-js/master/json.js"></script>*/ var j_str="[{key:\"key1\",val:11},{key:\"key2\",val:12},{key:\"key3\",val:13}]"; var jsonObj=eval('('+j_str+')'); var len=jsonObj.length; // 输出 len= 3 var jobj={}; jobj[0]={}; jobj[0].key="key1"; jobj[0].val=1; jobj[1]={}; jobj[1].key="key2"; jobj[1].val=2; jobj[2]={}; jobj[2].key="key3"; jobj[2].val=3; var str=JSON.stringify(jobj); /* str=[{"key":"key1","val":1},{"key":"key2","val":2},{"key":"key3","val":3}] 将 var jobj=new Array(); 换成 var jobj={}; JSON.stringify(jobj) 则 输出:{"0":{"key":"key1","val":1},"1":{"key":"key2","val":2},"2":{"key":"key3","val":3}} */