如何将嵌套的JSON数据结构展平为具有键值对的对象

时间:2017-01-12 15:05:36

标签: javascript json

来自源的字符串来自JSON对象中的碎片JSON对象。我想将此JSON结构转换为平面JSON结构

{
    "nest": {
        "a": {
            "b": {
                "h": {
                    "i": {
                        "all.css": "1",
                        "j": {
                            "k": {
                                "l": {
                                    "lr.png": "2",
                                    "c.png": "3"
                                },
                                ".png": "4"
                            }
                        }
                    }
                }
            }
        }
    }
}

我试过这样做,但我最多可以获得1个键值对, 我想要的是{"abhiall.css":"1","abhijkllr.png":"2","abhijklc.png":"3" ..等等。 感谢

4 个答案:

答案 0 :(得分:2)

您可以对嵌套对象使用迭代和递归方法。

function flatten(object, target, path) {
    path = path || '';
    Object.keys(object).forEach(function (key) {
        if (object[key] && typeof object[key] === 'object') {
            flatten(object[key], target, path + key);
            return;
        }
        target[path + key] = object[key];
    });
}

var data = { nest: { a: { b: { h: { i: { "all.css": "1", j: { k: { l: { "lr.png": "2", "c.png": "3" }, ".png": "4" } } } } } } } },
    flat = {};

flatten(data.nest, flat);
console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您可以使用递归函数返回所需的结果。

var obj = {"nest":{"a":{"b":{"h":{"i":{"all.css":"1","j":{"k":{"l":{"lr.png":"2","c.png":"3"},".png":"4"}}}}}}}}

function flat(data, c) {
  var result = {}
  for(var i in data) {
    if(typeof data[i] == 'string') result[c + i] = data[i]
    if(typeof data[i] == 'object') result = Object.assign(result, flat(data[i], c+= i))
  }
  return result
}


console.log(flat(obj.nest, ''))

答案 2 :(得分:0)

对于对象数组,我执行了以下操作 注意:确保传入的Objects Array至少为n> 0

flatten(objects, target, path) {
let me = this;    
let retArray = [];
for(let x=0; x < objects.length; x++) {

  let object = objects[x];
  path = path || '';
  target={};

  target = me.flattenHelper(object, target, path);

  retArray.push(target);
}
return retArray;}

...

flattenHelper(object, target, path){
let me = this;
Object.keys(object).forEach(function (key) {
  console.log("key : "+ key + " : object : " +  (object[key] && typeof object[key] === 'object') + " path : " + path);
  if (object[key] && typeof object[key] === 'object') {
      me.flattenHelper(object[key], target, path + key);
  }
  target[path + key] = object[key];
  console.log(target);
});
return target;}

答案 3 :(得分:0)

使用此功能:

const flattenObject = (obj, prefix = '') =>
  Object.keys(obj).reduce((acc, k) => {
    const pre = prefix.length ? prefix : '';
    if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k));
    else acc[pre + k] = obj[k];
    return acc;
  }, {});


console.log(flattenObject({"nest": {"a": {"b": {"h": {"i":{"all.css":"1","j": {"k": {"l": {"lr.png": "2","c.png": "3"},".png": "4"}}}}}}}}));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}