简体   繁体   English

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

[英]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.

相关问题 在JavaScript中将字符串转换为整数数组,反之亦然 - Convert string to array of integers and vice versa in JavaScript 如何在javascript中将整数数组转换为十六进制字符串,反之亦然 - how to convert array of integers into hex string and vice versa in javascript Javascript Cookies - 它们能否被服务器端脚本语言访问,反之亦然? - Javascript Cookies - Can they be accessed by a server side scripting language, visa versa? 在javascript中将任何字符串转换为数组的最快方法 - Fastest way to convert any string to array in javascript 将字符串值转换为Javascript数组的优雅方法? - Elegant way to convert string of values into a Javascript array? 将字符串转换为 Javascript 中的字符数组的推荐方法 - Recommended way to convert String into Array of chars in Javascript 在javascript中将字符串转换为对象数组的最佳方法? - Best way to convert string to array of object in javascript? JavaScript-以干净的方式将字节转换为浮点数 - JavaScript - Convert bytes into float in a clean way javascript有没有办法以干净的方式多次清理字符串 - javascript is there a way to clean up a string multiple times in a clean way 从 javascript 中的字符串中删除最后一个 & 的干净方法 - Clean way of removing last & from a string in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM