0

我需要帮助转换此数据表:

[
    {property:"key", content:"1"},
    {property:"key", content:"2"},
    {property:"key2", content:"3"},
    {property:"key2:subkey", content:"4"},
    {property:"key2:someother:key", content:"5"},
    {property:"foo", content:"6"},
    {property:"foo", content:"7"},
    {property:"foo:bar", content:"8"}
]

转换为具有以下结构的 JSON 对象:

{
  key: ["1", "2"],
  key2: {
    '': "3"
    subkey: "4"
    someother: {
      key: "5"
    }
  },
  foo: [
    "6",
    {
      '': "7"
      bar: "8"
    }
  ]
}

这是规则。注意:所有规则都适用于 JSON 对象中的任何级别(json.levelOne、json.level.two、json.level.three.even 等)

  1. 对于每个row.property喜欢"a:b:c"都应该翻译成json.a.b.c = row.content.
  2. 何时row.property = "x"json.x !== undefinedjson.x = [json.x, row.content]
  3. 无论何时json.x === "string"row.property = "x:y"json.x = {'': json.x, y: row.content}
  4. 无论何时Array.isArray(json.x) && json.x[json.x.length-1] === "string"row.property = "x:y"json.x[json.x.length-1] = {'': json.x[json.x.length-1], y: row.content}

希望这能让您对将数据转换为此 JSON 对象格式所需的标准有所了解。

为什么?

我正在尝试获取 Open Graph 元数据并将其序列化为 JSON 对象。我觉得上面的格式最能反映 Open Graph 元数据结构。我需要帮助来编写这个算法。这是针对我正在开发的一个开源 Node.js 项目。

感谢所有帮助。谢谢!

编辑

所以解析器还有一些问题。在某些情况下,数组出现在叶节点处。

这是 GitHub 上的项目:https ://github.com/samholmes/node-open-graph随意分叉它,构建更好的解析,并向我发送拉取请求。

4

1 回答 1

1

根据我们对 IRC 的讨论进行了更新

var data = [
    {property:"key", content:"1"},
    {property:"key", content:"2"},
    {property:"key2", content:"3"},
    {property:"key2:subkey", content:"4"},
    {property:"key2:someother:key", content:"5"},
    {property:"foo", content:"6"},
    {property:"foo", content:"7"},
    {property:"foo:bar", content:"8"},
    {property:"foo:baz", content:"9"}
];

var transformed = {};

data.forEach(function (item) {
    var key, tmp,
        ptr = transformed,
        keys = item.property.split(':');

    // we want to leave one key to assign to so we always use references
    // as long as there's one key left, we're dealing with a sub-node and not a value

    while (keys.length > 1) {
        key = keys.shift();

        if (Array.isArray(ptr[key])) {
            // the last index of ptr[key] should become
            // the object we are examining.
            tmp = ptr[key].length-1;
            ptr = ptr[key];
            key = tmp;
        }

        if (typeof ptr[key] === 'string') {
            // if it's a string, convert it
            ptr[key] = { '': ptr[key] };
        } else if (ptr[key] === undefined) {
            // create a new key
            ptr[key] = {};
        }

        // move our pointer to the next subnode
        ptr = ptr[key];
    }

    // deal with the last key
    key = keys.shift();

    if (ptr[key] === undefined) {
        ptr[key] = item.content;
    } else if (Array.isArray(ptr[key])) {
        ptr[key].push(item.content);
    } else {
        ptr[key] = [ ptr[key], item.content ];
    }
});
console.log(transformed);

输出:

{
    key: ['1', '2'],
    key2: {
        '': '3',
        subkey: '4',
        someother: {
            key: '5'
        }
    },
    foo: ['6', {
        '': '7',
        bar: '8'
        baz: '9'
    }]
}
于 2013-08-16T05:21:29.320 回答