如何在没有导入方法的情况下将json数据转换为纯lua中的xml

时间:2015-09-17 19:20:35

标签: lua

使用以下JSON文本,我需要使用Lua将此文本转换为XML表示,而不导入任何其他依赖项或方法。 Lua代码在进行这种转换时会是什么样子?

{"RS":"0","RSDESC":"SUCESS GAMES AVAILABLE","gamedata": [
    {"BetTypes":"0,0,0","BottomMessage":"WELCOME","DrawCloseTime":"57600","DrawDate":"09\/17\/2015","DrawID":"26091715","DrawNo":"109","DrawTime":"04:15 PM","GameGroupID":"1","LotName":"BANK3 - 04:00 PM","MRP":"10000","TktValidity":"17\/10\/2015"},
    {"BetTypes":"0,0,0","BottomMessage":"WELCOME","DrawCloseTime":"75600","DrawDate":"09\/17\/2015","DrawID":"27091715","DrawNo":"109","DrawTime":"09:15 PM","GameGroupID":"1","LotName":"BANK3 - 09:00 PM","MRP":"10000","TktValidity":"17\/10\/2015"}
    ]
}

1 个答案:

答案 0 :(得分:0)

试试这个。

J=[[
{"RS":"0","RSDESC":"SUCESS GAMES AVAILABLE","gamedata": [
    {"BetTypes":"0,0,0","BottomMessage":"WELCOME","DrawCloseTime":"57600","DrawDate":"09\/17\/2015","DrawID":"26091715","DrawNo":"109","DrawTime":"04:15 PM","GameGroupID":"1","LotName":"BANK3 - 04:00 PM","MRP":"10000","TktValidity":"17\/10\/2015"},
    {"BetTypes":"0,0,0","BottomMessage":"WELCOME","DrawCloseTime":"75600","DrawDate":"09\/17\/2015","DrawID":"27091715","DrawNo":"109","DrawTime":"09:15 PM","GameGroupID":"1","LotName":"BANK3 - 09:00 PM","MRP":"10000","TktValidity":"17\/10\/2015"}
    ]
}
]]

X=J:gsub('{','<ITEM>\n'):gsub('},?','</ITEM>\n')
X=X:gsub('"(%w-)"%s*:%s*(%b[])',
    function (k,v)
        return string.format("<%s>%s</%s>\n",k,v:sub(2,-2),k)
    end)
X=X:gsub('"(%w-)"%s*:%s*"(.-)",?',
    function (k,v)
        return string.format("<%s>%s</%s>\n",k,v,k)
    end)

print(X)