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

时间:2021-02-07 07:37:10

The strings that are coming from source is coming in fragmented JSON object in JSON object.I want to convert this JSON structure to flat JSON structure

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

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

I tried to do it but I could only get up to 1 key value pair, what i want is {"abhiall.css":"1","abhijkllr.png":"2","abhijklc.png":"3" ..and so on. Thanks

我试图这样做,但我只能得到1个键值对,我想要的是{“abhiall.css”:“1”,“abhijkllr.png”:“2”,“abhijklc.png”:“3 “ ..等等。谢谢

3 个解决方案

#1


1  

You could use an iterative and recursive approach for nested objects.

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

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; }

#2


1  

You can user recursive function to return desired result.

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

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, ''))

#3


0  

For array of objects I did the following Note : make sure the Objects Array passed in is n>0 at least

对于对象数组,我做了以下注意:确保传入的对象数组至少为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;}

#1


1  

You could use an iterative and recursive approach for nested objects.

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

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; }

#2


1  

You can user recursive function to return desired result.

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

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, ''))

#3


0  

For array of objects I did the following Note : make sure the Objects Array passed in is n>0 at least

对于对象数组,我做了以下注意:确保传入的对象数组至少为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;}