遍历JSON键值对

时间:2021-05-29 11:22:38
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类: