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
本文介绍如何在Lua环境中使用JSON模块进行数据的编码与解码,并演示了将Lua表转换为JSON字符串并保存到文件的过程,最后从文件中读取JSON数据并解析回Lua表。
815

被折叠的 条评论
为什么被折叠?



