我正在尝试将旧的 LUA 方法移动,该方法正在将一些 JSON 内容从文件加载到全局变量中到“类”中。但我一直收到以下错误:
attempt to call field 'decode' (a nil value)
attempt to index global 'cjson' (a nil value)
我不太了解lua,但我尝试了几乎所有没有结果的组合,所以你能解释为什么会出现这个错误吗?
模块的当前实现如下所示:
Config = {}
Config.__index = Config
function Config.create(config_filename)
local cjson = require("cjson")
local config = {}
setmetatable(config,Config)
local f = io.open(config_filename, "r")
local content = f:read("*a")
f:close()
config = cjson.decode(content)
return config
end
return Config
作为最终结果,我想从其他文件执行类似的操作:
local config_class = require("config")
local config = config_class.create("/path/to/file.json")
ngx.say(config:some_configuration_data())