lua保存table到文件并从文件解析成table

require("json")
result = {
    ["ip"]="192.168.0.177",
    ["date"]="2018-1-21",
         }

local test = assert(io.open("/tmp/abc.txt", "w"))
result = json.encode(result)
test:write(result)
test:close()


require("json")
local test = io.open("/tmp/abc.txt", "r")
local readjson= test:read("*a")
local table =json.decode(readjson)
test:close()
print("ip: " .. table["ip"])

参考:

https://my.oschina.net/u/223340/blog/382895

http://lua-users.org/wiki/SaveTableToFile

https://stackoverflow.com/questions/36834786/how-to-save-a-table-to-a-file-from-lua/48517263#48517263

posted @ 2018-01-31 09:56  船长博客  阅读(1678)  评论(0编辑  收藏  举报
永远相信美好的事情即将发生!