ubuntu1604 lua读取json文件

  • 创建环境

#sudo aptitude install lua-cjson

  • 实现

1)实现test.json

{
    "str":"hello world",
    "configs":[{
        "user":"ubuntu",
        "password":"123456",
        "ip":"....."
    },
    {
        "user":"ubuntu1",
        "password":"....",
        "ip":"testip"
    }
    ]
}

2)实现test.lua

function FileRead()
    local file = io.open("test.json","r")
    local json = file:read("*a");
    file:close()
    return json
end
local cjson = require "cjson"
local file = FileRead()
local json = cjson.decode(file)

for i,w in ipairs(json.configs) do
    print("user:"..w.user)
    print("password:"..w.password)
end
print("str:"..json.str)

3)运行
#lua5.1 test.lua

你可能感兴趣的:(ubuntu1604 lua读取json文件)