是否有一种简洁的方法将 Lua 表(作为字符串)转换为 JavaScript 数组?(反之亦然)
2019-9-25 20:23:42
收藏:0
阅读:132
评论:4
编辑:将 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 函数或库来完成这项工作,但我只找到了一些将 json 转换为 Lua 表的 Lua 库。 Lua 表将始终是字符串。是否存在一种方法来完成这个任务?
点赞
用户6224823
这里有一些你可能不知道的东西,{ ["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);
2019-09-25 20:39:52
用户1243641
这只是一个局部解决方案。在一些字符串内部匹配关键字语法的情况下可能会失败。但这可能并不是您所关注的。
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:45:32
用户1433693
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, '') // 去除中括号
.replace(/=/g, ':') // 替换等号为冒号
.replace(/(\,)(?=\s*})/g, '') // 去除末尾的逗号
.replace(/\{\s*(\"[^".]*\"(\s*\,\s*\"[^".]*\")*)\s*\}/g, '[$1]') // 转换为数组
console.log (result)
const parsed = JSON.parse(result);
console.log(parsed);
2022-09-14 10:51:44
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 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, '') // 删除括号 .replace(/=/g, ':') // 将 = 替换为 : .replace(/(\,)(?=\s*})/g, ''); // 删除末尾逗号 const parsed = JSON.parse(result); console.log(result);