为了账号安全,请及时绑定邮箱和手机立即绑定

是否存在将 lua 表(作为字符串)转换为 javascript 数组的干净方法?(反之亦然)

是否存在将 lua 表(作为字符串)转换为 javascript 数组的干净方法?(反之亦然)

冉冉说 2021-10-29 13:54:04
将 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 表总是在一个字符串中。有没有办法做到这一点?
查看完整描述

3 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

您可以手动执行以使其成为有效的 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);


查看完整回答
反对 回复 2021-10-29
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

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


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包装纸。


查看完整回答
反对 回复 2021-10-29
?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

这里有一些你可能不知道的东西,它{ ["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);


查看完整回答
反对 回复 2021-10-29
  • 3 回答
  • 0 关注
  • 182 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信