2013-04-27 219 views
5

这个EPGP World of Warcraft addon输出一个epgp.lua数据库文件。将Lua数据转换为JSON

我写了一个plugin将Lua数据转换成JSON对象以显示在公会网站上。它正在老版本的插件中工作,但现在我无法正确地转换文件。以下是两个显示转换问题的摘录 - 请参阅this demo

第一作品在形成嵌套数组极大:

["roster_info"] = { 
    { 
     "Agantica", -- [1] 
     "ROGUE", -- [2] 
     "09/03-2013", -- [3] 
    }, -- [1] 
    { 
     "Intikamim", -- [1] 
     "PALADIN", -- [2] 
     "17/02-2013", -- [3] 
    }, -- [2] 
}, 

变得

"roster_info" : [ 
    [ 
     "Agantica", 
     "ROGUE", 
     "09/03-2013" 
    ], 
    [ 
     "Intikamim", 
     "PALADIN", 
     "17/02-2013" 
    ] 
] 

但串replacment认为这下一个片段作为嵌套阵列时,它应是一个内部的对象阵列:

["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] 
}, 

变得

"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" 
    ] 
] 

以下是仅适用于第一个片段的字符串转换脚本。

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\=/,''); 

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

+0

[epgp.lua](https://github.com/Mottie/epgp/blob/master/epgp.lua)是如何生成的?如果它是生成此输出的lua代码,则可以编辑该代码并使用LuaJSON库/模块。 – hjpotter92 2013-04-27 18:59:58

+0

当你退出魔兽世界时,它是由插件生成的。您所做的只是将原始数据文件上传到您的网站。 – Mottie 2013-04-27 19:00:58

+0

这是因为你用'方括号替换集合结尾的评论'和'将双曲线改成方括号'行。双数组不是必须的数组。阵列中的对象也是Lua中的双曲。 – 2013-04-27 19:58:42

回答

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; 
+0

+1不错的答案,但遗憾的是它可以在webkit中使用,但不适用于Firefox:http://jsfiddle.net/Mottie/MfncJ/4/(使用完整的epgp.lua文件) - 是否可能是Firefox不支持匹配捕获群体? – Mottie 2013-04-28 16:26:06

+0

@Mottie - 这个字符串对于正则表达式操作来说太长了。 – 2013-04-28 17:11:02

+0

@Mottie - 或者JSON解析太长。 – 2013-04-28 17:23:03

7

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

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

+2

+1在正确的方向微调。如果数据使用者需要JSON,但是您有一个Lua表,那么正确的答案是首先从Lua代码生成JSON,而不是尝试执行文本替换,只有使用完整的Lua解析器才能成功。这实际上相当于让Lua首先编写JSON输出,这是一个解决的问题。 – RBerteig 2013-04-29 22:53:54