DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript基礎知識 >> JavaScript遍歷JSON鍵值對代碼
JavaScript遍歷JSON鍵值對代碼
編輯:JavaScript基礎知識     

jS遍歷JSON鍵值對
JSON既然是鍵值對應該可以像.net裡的Dictionary一樣可以遍歷才對。試試看:
    //遞歸遍歷JSON所有鍵值
    function recurJson(json) 
    { 
        for(var i in json){         
            document.write(i+":"+json[i]+" <br>");        
            if(typeof json[i]=="object"){ 
                recurJson(json[i]); 
            } 
        } 
    }

  

 var    dd={"a":"=a","b":"=b","c":"=c","d":{"d1":"=d1","d2":"=d2"}};
    dd.a="=a2";
    recurJson(dd);

 輸出:
a:=a2
b:=b
c:=c
d:[object Object]
d1:=d1
d2:=d2  

 var record=new Ext.data.Record({"a":"=a","b":"=b","c":"=c","d":{"d1":"=d1","d2":"=d2"}});
    //record.set("a","a=a");//修改鍵值
    //Ext.Msg.alert("提示", record.get("d").d1);//獲取鍵值    
    //recurJson(record.data);//遍歷Record的數據    
    recurJson(record);    //遍歷Record的配置項,配置項本身就是JSON格式的

輸出:
isModified:function(A){return this.modified&&this.modified.hasOwnProperty(A)}
copy:function(A){return new this.constructor(Ext.apply({},this.data),A||this.id)}
clearError:function(){this.error=null}
hasError:function(){return this.error!=null}
getChanges:function(){var A=this.modified,B={};for(var C in A){if(A.hasOwnProperty(C)){B[C]=this.data[C]}}return B}
commit:function(A){this.dirty=false;delete this.modified;this.editing=false;if(this.store&&A!==true){this.store.afterCommit(this)}}
reject:function(B){var A=this.modified;for(var C in A){if(typeof A[C]!="function"){this.data[C]=A[C]}}this.dirty=false;delete this.modified;this.editing=false;if(this.store&&B!==true){this.store.afterReject(this)}}
endEdit:function(){this.editing=false;if(this.dirty&&this.store){this.store.afterEdit(this)}}
cancelEdit:function(){this.editing=false;delete this.modified}
beginEdit:function(){this.editing=true;this.modified={}}
get:function(A){return this.data[A]}
set:function(A,B){if(String(this.data[A])==String(B)){return }this.dirty=true;if(!this.modified){this.modified={}}if(typeof this.modified[A]=="undefined"){this.modified[A]=this.data[A]}this.data[A]=B;if(!this.editing&&this.store){this.store.afterEdit(this)}}
join:function(A){this.store=A}
modified:null
error:null
editing:false
dirty:false
id:1001
data:[object Object]
a:=a
b:=b
c:=c
d:[object Object]
d1:=d1
d2:=d2
綜合以上:定義一個JSON類:

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved