亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否存在將 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 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號