這個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能夠正確轉換對象數組(第二個示例)。
[epgp.lua](https://github.com/Mottie/epgp/blob/master/epgp.lua)是如何生成的?如果它是生成此輸出的lua代碼,則可以編輯該代碼並使用LuaJSON庫/模塊。 – hjpotter92 2013-04-27 18:59:58
當你退出魔獸世界時,它是由插件生成的。您所做的只是將原始數據文件上傳到您的網站。 – Mottie 2013-04-27 19:00:58
這是因爲你用'方括號替換集合結尾的評論'和'將雙曲線改成方括號'行。雙數組不是必須的數組。陣列中的對象也是Lua中的雙曲。 – 2013-04-27 19:58:42