gpt4 book ai didi

javascript - 是否存在一种干净的方法将 lua 表(作为字符串)转换为 javascript 数组? (反之亦然)

转载 作者:行者123 更新时间:2023-12-01 00:36:17 25 4
gpt4 key购买 nike

编辑:将 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",
},
}

尽管我只遇到过一些将 json 转换为 lua 表的 lua 库,但我已经四处寻找一些 javascript 函数或库来执行此操作。 lua表总是在一个字符串中。有没有办法做到这一点?

最佳答案

这只是部分解决方案。在某些情况下,如果字符串内的文本与键语法匹配,则可能会失败。但这可能不是您关心的问题。

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 包装器。

关于javascript - 是否存在一种干净的方法将 lua 表(作为字符串)转换为 javascript 数组? (反之亦然),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58105883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com