编辑:将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表总是在一个字符串中。有没有办法做到这一点?
发布于 2019-09-25 20:19:10
您可以手动将其设置为有效的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);
发布于 2019-09-25 20:45:33
这只是一个部分的解决方案。在字符串中的文本与键语法匹配的某些情况下,它可能会失败。但这可能不是你关心的问题。
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
包装器。
发布于 2019-09-25 20:39:52
这里有一些你可能不知道的东西,{ ["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);
https://stackoverflow.com/questions/58105883
复制