是否有一种简洁的方法将 Lua 表(作为字符串)转换为 JavaScript 数组?(反之亦然)

编辑:将 Lua 表作为字符串,使用 JavaScript 将其转换为 JavaScript 数组。不在 Lua 中编程。

因此,Lua 表只是以不同格式的关联数组。

    -- LUA TABLE EXAMPLE
    {
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }

我已经寻找了一些 JavaScript 函数或库来完成这项工作,但我只找到了一些将 json 转换为 Lua 表的 Lua 库。 Lua 表将始终是字符串。是否存在一种方法来完成这个任务?

点赞
用户7328218
用户7328218

你可以手动将其转换为有效的 JSON 格式,然后再解析它:

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }}`;

const result = luaStr
  .replace(/\[|\]/g, '') // 删除括号
  .replace(/=/g, ':') // 将 = 替换为 :
  .replace(/(\,)(?=\s*})/g, ''); // 删除末尾逗号

const parsed = JSON.parse(result);

console.log(result);
2019-09-25 20:19:09
用户6224823
用户6224823

这里有一些你可能不知道的东西,{ ["someString"]: 2 } 是有效的 javascript 并会被计算为 {someString: 2},它也是有效的 json。唯一的问题是它需要被计算,这意味着需要使用 eval(如果可以避免的话,确实不应该这样做)。

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }}`;

const jsonString = luaStr.replace(/\] = /g, ']: ');
const jsonObj = eval(`(${jsonString})`);

console.log(jsonObj);
2019-09-25 20:39:52
用户1243641
用户1243641

这只是一个局部解决方案。在一些字符串内部匹配关键字语法的情况下可能会失败。但这可能并不是您所关注的。

const lua = `
{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }
}`

const lua2json = lua =>
  JSON.parse(lua.replace(
    /\[([^\[\]]+)\]\s*=/g,
    (s, k) => `${k}:`
  )
  .replace(/,(\s*)\}/gm, (s, k) => `${k}}`))

console.log(
  lua2json(lua)
)

我不知道您是否想要创建JSON还是对象。我选择了后者,但您可以删除 JSON.parse 包装器。

2019-09-25 20:45:32
用户1433693
用户1433693

Added array converting

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["test1"] = { "test1"
     },

        ["test2"] = { "test1",
         "test2" },

        ["test3"] = { "test1", "test2", "test2" },
    }}`;

const result = luaStr
.replace(/\[|\]/g, '') // 去除中括号
.replace(/=/g, ':') // 替换等号为冒号
.replace(/(\,)(?=\s*})/g, '') // 去除末尾的逗号
.replace(/\{\s*(\"[^".]*\"(\s*\,\s*\"[^".]*\")*)\s*\}/g, '[$1]') // 转换为数组

console.log (result)

const parsed = JSON.parse(result);
console.log(parsed);
2022-09-14 10:51:44