[英]Does there exist a clean way to convert a lua table (as a string) to a javascript array? (and visa versa)
EDIT: Take a lua table as a string, and use javascript to convert it to a javascript array.编辑:将 lua 表作为字符串,并使用 javascript 将其转换为 javascript 数组。 No programming in lua.
lua 中没有编程。
So a lua table is just an associative array in a different format.所以 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",
},
}
I've looked around for some javascript functions or libraries to do this though I've only come across some lua libraries to convert json to a lua table.我四处寻找一些 javascript 函数或库来执行此操作,尽管我只遇到了一些 lua 库来将 json 转换为 ZF890ZABBDB3B878F751 表。 The lua table will always be in a string.
lua 表将始终位于字符串中。 Does there exist a way of doing this?
有没有办法做到这一点?
You can to do it manually to make it a valid JSON then parse it:您可以手动执行以使其成为有效的 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);
This is only a partial solution.这只是部分解决方案。 It could fail in some cases where the text inside a string matches the key syntax.
在字符串中的文本与键语法匹配的某些情况下,它可能会失败。 But that may not be a concern for you.
但这对您来说可能不是问题。
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) )
I didn't know if you were looking to create JSON or an object.我不知道您是要创建 JSON 还是 object。 I chose the latter, but you could always remove the
JSON.parse
wrapper.我选择了后者,但您始终可以删除
JSON.parse
包装器。
Here is something that you might not know, { ["someString"]: 2 }
is valid javascript and will evaluate to {someString: 2}
which is valid json.这是您可能不知道的,
{ ["someString"]: 2 }
是有效的 javascript 并且将评估为{someString: 2}
这是有效的 json。 The only issue is it needs to be evaluated, which means using eval
(which you really never should, if you can avoid it).唯一的问题是它需要被评估,这意味着使用
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);
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, '') // 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.