2013-04-27 606 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