javascript - 在javascript中将类似URL的数据解析为json

标签 javascript json

谁能告诉我如何将类似 URL 的数据数组解析为 json?

数组["a.b.c.d","a.c.e.f","a.b.c.g"]到这种json:

items:{
    text: "a",
    items:[
        {
            text:"b",   
            items:[
            {
                text:"c",
                items:[
                {
                    text:"d",
                    leaf:true
                },
                {
                    text:"g",
                    leaf:true
                }
                ]

            }
            ]
        },
        {
            text:"c",
            items:[
            {
                text:"e",
                items:[
                {
                    text:"f",
                    leaf:true
                }
                ]
            }
            ]
        }
    ]
}

最佳答案

以下应该有效:

// ['a', 'b'] -> { text: 'a', items: [{ text: 'b', leaf: true }] }
function buildTree(components) {
  if (components.length === 0) {
    throw new Error('Can\'t parse: empty components');
  } else if (components.length === 1) {
    return { text: components[0], leaf: true };
  } else {
    return {
      text: components[0],
      items: [buildTree(components.slice(1))]
    }
  }
}

// 'a.b' -> { text: 'a', items: [{ text: 'b', leaf: true }] }
function parseString(str) {
  return buildTree(str.split('.'));
}

// Merge nodes with a same text.
function mergeSame(left, right) {
  if (left.text !== right.text) {
    throw new Error('Can\'t merge: different text ' + left.text + ', ' + right.text);
  }

  // Same text
  if (left.leaf && right.leaf) {
    return left;
  } else if (left.leaf && !right.leaf) {
    return right;
  } else if (!left.leat && right.leaf) {
    return left;
  } else {
    var concat = left.items.concat(right.items);
    return { text: left.text, items: merge(concat) };
  }
}

// Merge multiple nodes.
function merge(items) {
  var textToItem = {};
  var keys = [];
  for (var i = 0; i < items.length; i++) {
    var text = items[i].text;
    if (textToItem[text]) {
      textToItem[text] = mergeSame(textToItem[text], items[i]);
    } else {
      textToItem[text] = items[i];
      keys.push(text);
    }
  }
  keys.sort();
  var merged = [];
  for (i = 0; i < keys.length; i++) {
    merged.push(textToItem[keys[i]]);
  }
  return merged;
}

function parse(strs) {
  var nodes = [];
  for (var i = 0; i < strs.length; i++) {
    nodes.push(parseString(strs[i]));
  }
  return { items: merge(nodes) };
}

console.log(parseString('a.b.c.d'));

console.log(parse(["a.b.c.d","a.c.e.f","a.b.c.g"]));

它可能看起来很乱。我不确定您的环境,也没有使用 mapreduce

关于javascript - 在javascript中将类似URL的数据解析为json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21775895/

相关文章:

javascript - Github Pages 仅在选项卡之间切换时重新加载

javascript - 未评估第二个变量

json - 为什么 JSON 文件比具有相同内容的 .txt 文件小很多?

json - 查询嵌入式文档数组给出错误答案

javascript - 如何使用在另一个 javascript 文件中调用的函数的 JSON 结果

json - 在 Windows 上的 Excel VBA 中,对于解析的 JSON 变量,这个 JScriptTypeInfo 到底是什么?

javascript - 分配给原型(prototype)属性不应该在原型(prototype)上覆盖它吗?

Javascript Function.prototype.call() 传递原始值

Javascript - 从输入中删除焦点

javascript - JSON.parse node.js 中的意外结果