我有一个字符串形式的表数据。示例如下:
{"engName1":"HOLDER","validDurPeriod":3,"engName2":"INFORMATION","appStatus":2,"stayExpDate":"01/10/2012","engName3":"","appExpDate":"12/04/2010"}
我怎样才能将它转换成一个合适的表类型变量,以便我可以访问键。我是 lua 的新手,我不知道是否有任何现有的方法可以这样做。
最佳答案
有plenty可用于 Lua 的 JSON 解析器,例如 dkjson :
local json = require ("dkjson")
local str = [[
{
"numbers": [ 2, 3, -20.23e+2, -4 ],
"currency": "\u20AC"
}
]]
local obj, pos, err = json.decode (str, 1, nil)
if err then
print ("Error:", err)
else
print ("currency", obj.currency)
for i = 1,#obj.numbers do
print (i, obj.numbers[i])
end
end
输出:
currency €
1 2
2 3
3 -2023
4 -4
关于lua - 如何在Lua中将字符串转换成表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757935/