如何从具有不同嵌套的json以指定的方式反对值

时间:2018-07-09 17:29:41

标签: javascript jquery node.js html5

我想以特定的方式从array构建json

这是我的json

var data = {
    "first": {
        "dial": {
            "ss": true,
            "tt": false
        },
        "report": {
            "rr": true,
            "mm": false
        },
        "media": {
            "media": false,
        }
    },
    "second": {
        "media": {
        },
        "order": {
            "cg_counter": true,
            "sub_vertical": false
        },
        "rrt":{
           "mtnl":{
              "gistn":12344,
              "qt":"testing",
           }
        }
    }
};

我期望我的输出(下一个):

  

第一
      拨打:ss,tt

     

报告:rr,毫米

     

媒体:媒体

     

秒:
     媒体:

     

顺序:cg_counter,sub_vertical

     

rrt:mtnl:gistn,qt

我尝试过像下面这样的编码(我不知道我必须做多少层嵌套)。

for(var key in data)
{

     console.log(key.toUpperCase() + ' : ');

    if(typeof data[key] == 'object'){

         for(anotherKey in data[key]){
             console.log(data[key][anotherKey]);
         }
    }
}

1 个答案:

答案 0 :(得分:0)

也许是这样的:

var data = {
    "first": {
        "dial": {
            "ss": true,
            "tt": false
        },
        "report": {
            "rr": true,
            "mm": false
        },
        "media": {
            "media": false,
        }
    },
    "second": {
        "media": {
        },
        "order": {
            "cg_counter": true,
            "sub_vertical": false
        },
        "rrt":{
           "mtnl":{
              "gistn":12344,
              "qt":"testing",
           }
        }
    }
};

function get_all_data(_data){
   for(var key in _data){
     if (typeof _data[key] == "object" && _data[key] !== null){
 	        console.log(key +' => '+ 'object restart function!');
 	        get_all_data(_data[key]);
     }else{
 	        console.log(' '+key + ' => ' + _data[key]);
     }
   }
}

get_all_data(data);