递归遍历
删除的话可以用delete xx.xx

解决方案 »

  1.   

    你可以用数组的思想理解Json就容易了,
    给个代码你看看,没考虑优化问题,但是遍历子孙对象的属性特别有用function find(obj)
    {
    for(var i in obj)
    {
    if(obj[i]!=null&&typeof(obj[i])!="function"&&typeof(obj[i])=="object")
    {
    find(obj[i]);


    }
    //这里添加你需要属性操作的代码
    }}
    find(json);至于删除对象属性,使用delete运算符就可以了,删除指定的
      

  2.   


    我把全部代码贴出来吧
    <script type="text/javascript">
    function showJSON() {  
    var json={
    "name":"zhansan",
    "age":23,
    "address":{
    "city":"beijing",
    "year":["气态流出物月报",0,1],
    "gas":{
    "gasSheet":"气态流出物月报",
    "H_adjust":1
    },
    "time":{
    "year": ["气态流出物月报",0,1],
    "start":[1,"~"],
    "duration":31
    }
    },
    "units":{"title":"function"}, //怎么判断它是相对于根的子节点?
    "student":[13,"s1","s2"]
    }
    var root="json";
    var count=0;
    var flag=false;
    forTree=function(json){
    for(var i in json){
    if(typeof json[i] == "object"){ //有子节点
    if(json[i].length >=0 ){ //数组
    var nodes;
    if(flag){
    nodes=root+"."+i;
    }else{
    nodes="json."+i;
    }
    for(var l=0;l<json[i].length;l++){
    alert(nodes+"["+l+"]"+" : "+json[i][l]);
    }
    }else{ //是对象
    flag=true;
    //iteration=true;
    if(count > 0){ //迭代过一次,root截掉最后一个节点,接上新节点
    root=root.substring(0,root.lastIndexOf("."))+"."+i;
    }else{ //没有迭代过,root就用上一次的
    root=root+"."+i;
    }
    forTree(json[i]);
    flag=false;
    count++;
     forTree(json);
    }  
    </script> 
    <body>
    <input type="button" onclick="showJSON()" value="ShowJson" />
    </body>
    现在问题是“"units":{"title":"function"}”之前的都没有问题,但是到了这里,就打印“json.address.units.title : function”,这里本该是打印“json.units.title : function”的,然后下一个节点也没有问题,打印的是“json.student[0] : 13”...这该怎么解决呢?
      

  3.   


    我把全部代码贴出来吧
    <script type="text/javascript">
    function showJSON() {  
    var json={
    "name":"zhansan",
    "age":23,
    "address":{
    "city":"beijing",
    "year":["气态流出物月报",0,1],
    "gas":{
    "gasSheet":"气态流出物月报",
    "H_adjust":1
    },
    "time":{
    "year": ["气态流出物月报",0,1],
    "start":[1,"~"],
    "duration":31
    }
    },
    "units":{"title":"function"}, //怎么判断它是相对于根的子节点?
    "student":[13,"s1","s2"]
    }
    var root="json";
    var count=0;
    var flag=false;
    forTree=function(json){
    for(var i in json){
    if(typeof json[i] == "object"){ //有子节点
    if(json[i].length >=0 ){ //数组
    var nodes;
    if(flag){
    nodes=root+"."+i;
    }else{
    nodes="json."+i;
    }
    for(var l=0;l<json[i].length;l++){
    alert(nodes+"["+l+"]"+" : "+json[i][l]);
    }
    }else{ //是对象
    flag=true;
    //iteration=true;
    if(count > 0){ //迭代过一次,root截掉最后一个节点,接上新节点
    root=root.substring(0,root.lastIndexOf("."))+"."+i;
    }else{ //没有迭代过,root就用上一次的
    root=root+"."+i;
    }
    forTree(json[i]);
    flag=false;
    count++;
     forTree(json);
    }  
    </script> 
    <body>
    <input type="button" onclick="showJSON()" value="ShowJson" />
    </body>
    现在问题是“"units":{"title":"function"}”之前的都没有问题,但是到了这里,就打印“json.address.units.title : function”,这里本该是打印“json.units.title : function”的,然后下一个节点也没有问题,打印的是“json.student[0] : 13”...这该怎么解决呢?
    代码贴错了,应该是这样:
    <script type="text/javascript">
    function showJSON() {  
    var json={
    "name":"zhansan",
    "age":23,
    "address":{
    "city":"beijing",
    "year":["气态流出物月报",0,1],
    "gas":{
    "gasSheet":"气态流出物月报",
    "H_adjust":1
    },
    "time":{
    "year": ["气态流出物月报",0,1],
    "start":[1,"~"],
    "duration":31
    }
    },
    "units":{"title":"function"}, //怎么判断它是相对于根的子节点?
    "student":[13,"s1","s2"]
    }

    //alert(json["address"]["year"]);
    var root="json";
    var count=0;
    var flag=false;
    forTree=function(json){
    for(var i in json){
    if(typeof json[i] == "object"){ //有子节点
    if(json[i].length >=0 ){ //数组
    var nodes;
    if(flag){
    nodes=root+"."+i;
    }else{
    nodes="json."+i;
    }
    for(var l=0;l<json[i].length;l++){
    alert(nodes+"["+l+"]"+" : "+json[i][l]);
    }
    }else{ //是对象
    flag=true;
    //iteration=true;
    if(count > 0){ //迭代过一次,root截掉最后一个节点,接上新节点
    root=root.substring(0,root.lastIndexOf("."))+"."+i;
    }else{ //没有迭代过,root就用上一次的
    root=root+"."+i;
    }
    forTree(json[i]);
    flag=false;
    count++;
    }
    }else{
    alert(root+"."+i+" : "+json[i]);
    }
    }
    }
    forTree(json);
    }  
    </script> 
    <body>
    <input type="button" onclick="showJSON()" value="ShowJson" />
    </body>
      

  4.   

    楼主可以再迭代的时候带入参数,这样应该就不会有节点沿用上一个节点的问题,forTree(json,nodes),每次迭代nodes要先清一下
      

  5.   

    递归遍历就行了,增加path参数    function showJSON() {
            var json = {
                "name": "zhansan",
                "age": 23,
                "address": {
                    "city": "beijing",
                    "year": [{ test: "气态流出物月报" }, [0, [1, 1]], 1],
                    "gas": {
                        "gasSheet": "气态流出物月报",
                        "H_adjust": 1
                    },
                    "time": {
                        "year": ["气态流出物月报", 0, 1],
                        "start": [1, "~"],
                        "duration": 31
                    }
                },
                "units": { "title": "function" },    //怎么判断它是相对于根的子节点?
                "student": [13, "s1", "s2"]
            }
            function RecursionJson(json, path) {
                var isArray = json.length;
                for (var attr in json)
                    if (typeof json[attr] == 'object') {
                        if (json[attr].length) {
                            for (var j = 0; j < json[attr].length; j++) {
                                if (typeof json[attr][j] == 'object')
                                    RecursionJson(json[attr][j], path + '.' + attr + '[' + j + ']'); //不是普通类型,递归遍历
                                else alert(path + (isArray ? '[' : '.') + attr + (isArray ? ']' : '') + '[' + j + ']:' + json[attr][j]);
                            }
                        }
                        else RecursionJson(json[attr], path + "." + attr); //递归遍历
                    }
                    else alert(path + (isArray ? '[' : '.') + attr + (isArray ? ']' : '') + ':' + json[attr]);
            }        RecursionJson(json, "json");
        }