[英]Does there exist a clean way to convert a lua table (as a string) to a javascript array? (and visa versa)
編輯:將 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 轉換為 ZF890ZABBDB3B878F751 表。 lua 表將始終位於字符串中。 有沒有辦法做到這一點?
您可以手動執行以使其成為有效的 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 還是 object。 我選擇了后者,但您始終可以刪除JSON.parse
包裝器。
這是您可能不知道的, { ["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);
添加了數組轉換
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, '') // remove the brackets.replace(/=/g, ':') // replace the = with: .replace(/(\,)(?=\s*})/g, '') // remove trailing commas.replace(/\{\s*(\"[^".]*\"(\s*\,\s*\"[^".]*\")*)\s*\}/g, '[$1]') // to array console.log (result) const parsed = JSON.parse(result); console.log(parsed);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.