将Lua数据转换为JSON

Mot*_*tie 5 javascript lua replace world-of-warcraft

这个EPGP魔兽世界插件输出一个epgp.lua数据库文件.

我写了一个插件,将Lua数据转换为JSON对象,以便在公会网站上显示.它在旧版本的插件中工作,但现在我无法让它正确地转换文件.以下是显示转换问题的两个片段 - 请参阅此演示.

第一个非常适合形成嵌套数组:

["roster_info"] = {
    {
        "Agantica", -- [1]
        "ROGUE", -- [2]
        "09/03-2013", -- [3]
    }, -- [1]
    {
        "Intikamim", -- [1]
        "PALADIN", -- [2]
        "17/02-2013", -- [3]
    }, -- [2]
},
Run Code Online (Sandbox Code Playgroud)

"roster_info" : [
    [
        "Agantica",
        "ROGUE",
        "09/03-2013"
    ],
    [
        "Intikamim",
        "PALADIN",
        "17/02-2013"
    ]
]
Run Code Online (Sandbox Code Playgroud)

但是当字符串替换应该是数组中的对象时,字符串替换将此下一个片段视为嵌套数组:

["bonus_loot_log"] = {
    {
        ["player"] = "Magebox",
        ["timestamp"] = "2013-03-07 13:44:00",
        ["coinsLeft"] = "-1",
        ["reward"] = "|cffa335ee|Hitem:86815:0:0:0:0:0:0:632235520:90:0:445|h[Attenuating Bracers]|h|r",
    }, -- [1]
            {
        ["player"] = "Lîutasila",
        ["coinsLeft"] = "-1",
        ["timestamp"] = "2013-03-07 13:47:00",
    }, -- [2]
},
Run Code Online (Sandbox Code Playgroud)

"bonus_loot_log" : [
    [
        "player" : "Magebox",
        "timestamp" : "2013-03-07 13:44:00",
        "coinsLeft" : "-1",
        "reward" : "|cffa335ee|Hitem:86815:0:0:0:0:0:0:632235520:90:0:445|h[Attenuating Bracers]|h|r"
    ],
    [
        "player": "Lîutasila",
        "coinsLeft": "-1",
        "timestamp": "2013-03-07 13:47:00"
    ]
]
Run Code Online (Sandbox Code Playgroud)

以下是仅适用于第一个代码段的字符串转换脚本.

lua_string
    .replace(/\[(.*)\]\s\=\s/g,'$1:')     // change equal to colon & remove outer brackets
    .replace(/[\t\r\n]/g,'')              // remove tabs & returns
    .replace(/\}\,\s--\s\[\d+\]\}/g,']]') // replace sets ending with a comment with square brackets
    .replace(/\,\s--\s\[\d+\]/g,',')      // remove close subgroup and comment
    .replace(/,(\}|\])/g,'$1')            // remove trailing comma
    .replace(/\}\,\{/g,'],[')             // replace curly bracket set with square brackets
    .replace(/\{\{/g,'[[')                // change double curlies to square brackets
    .replace(/EPGP_DB\s\=/,'');
Run Code Online (Sandbox Code Playgroud)

所以,我需要一些帮助让Lua正确转换对象数组(第二个例子).

Mic*_*man 8

您通常无法使用字符串操作将任何Lua表转换为JSON数据.问题是,虽然Lua对数组和字典使用表,但JSON需要两种不同的类型.还有其他语法差异.

这最好通过在Lua和JSON表示之间进行转换的模块来解决.看看JSON模块上的Lua wiki并找到一个Lua模块将Lua转换为JSON.有多个模块,其中一些是纯Lua,是嵌入WoW的不错选择.它们正确检测表是否表示数组或字典并输出相关的JSON.

  • 在正确的方向上轻推+1.如果数据使用者需要JSON,但你有一个Lua表,那么正确的答案是首先从Lua代码生成JSON,而不是尝试进行文本替换,只有在使用完整的Lua解析器时才能成功.这真的等于让Lua首先编写JSON输出,这是一个解决的问题. (2认同)

Ego*_*off 1

// convert EPGP_DB from LUA to JSON
var str = document.getElementsByTagName('data')[0].innerHTML;
var diff;
do {  // replace curlies around arrays with square brackets
    diff = str.length;
    str = str.replace(/\{(((\n\t*)\t)\S.*(\2.*)*)\,\s--\s\[\d+\]\3\}/g,'[$1$3]');
    diff = diff - str.length;
} while (diff > 0);
str = str
.replace(/EPGP_DB\s=\s/, '')         // remove variable definition
.replace(/\s--\s\[\d+\](\n)/g, '$1') // remove comment
.replace(/\,(\n\t*\})/g, '$1')       // remove trailing comma
.replace(/\[(.*?)\]\s\=\s/g,'$1:')   // change equal to colon, remove brackets
.replace(/[\t\r\n]/g,'');            // remove tabs & returns
console.log(str);
json = window.JSON.parse(str);
console.log(json);
document.getElementById('result').innerText = json.global.last_version;
Run Code Online (Sandbox Code Playgroud)