是否存在将 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 函数或库来执行此操作,尽管我只遇到了一些 lua 库来将 json 转换为 lua 表。lua 表总是在一个字符串中。有没有办法做到这一点?


冉冉说
浏览 183回答 3
3回答

慕后森

您可以手动执行以使其成为有效的 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, '') // remove the brackets  .replace(/=/g, ':') // replace the = with :  .replace(/(\,)(?=\s*})/g, ''); // remove trailing commas  const parsed = JSON.parse(result);console.log(result);

猛跑小猪

这只是部分解决方案。在某些情况下,如果字符串中的文本与关键语法匹配,它可能会失败。但这可能不是你关心的问题。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包装纸。

慕无忌1623718

这里有一些你可能不知道的东西,它{ ["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);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript