【发布时间】:2013-04-21 18:27:15
【问题描述】:
这个EPGP World of Warcraft addon 输出一个epgp.lua 数据库文件。
我写了一个plugin 来将 Lua 数据转换为 JSON 对象以显示在公会网站上。它在旧版本的插件中工作,但现在我很难让它正确转换文件。这里有两个显示转换问题的 sn-ps - 请参阅 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"
]
]
但字符串替换将下一个 sn-p 视为嵌套数组,而它应该是数组内的对象:
["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"
]
]
这里是只对第一个sn-p有效的字符串转换脚本。
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 能够正确转换对象数组(第二个示例)。
【问题讨论】:
-
epgp.lua 是如何生成的?如果是生成此输出的 lua 代码,您可以编辑该代码并使用 LuaJSON 库/模块。
-
登出魔兽世界时插件生成。您只需将原始数据文件上传到您的网站。
-
这是因为您的
replace sets ending with a comment with square brackets和change double curlies to square brackets行。双卷曲不是必须的数组内的数组。数组中的对象在 Lua 中也是双花括号。 -
@EgorSkriptunoff 您能否用您所描述的内容更新演示。我遇到的问题是将对象与数组内部的数组区分开来,或者可能有我没有想到的更好的方法?
标签: javascript replace lua world-of-warcraft