lua 安装cjson

下载cjson

git clone https://github.com/mpx/lua-cjson

cd lua-cjson

make

这一步可能会出错, 查找lua.h的位置

sudo find / -name lua.h

=> /usr/local/include/luajit-2.0/lua.h

修改 Makefile

LUA_INCLUDE_DIR = /usr/local/include/luajit-2.0

继续编译,需要有特定的权限

make && make install

测试一把

local cjson2 = require "cjson"
-- 布尔类型
local lua_bool = true
print(cjson2.encode(lua_bool))

-- 数组类型
local lua_array = {1, 2, 3, 4, 5, 6}
print(cjson2.encode(lua_array))

-- 数值类型
local lua_number = 6.66
print(cjson2.encode(lua_number))

-- 字符串类型
local lua_string = "I am test1280"
print(cjson2.encode(lua_string))

-- 对象类型
local lua_object = {
    ["name"] = "Jiang",
    ["age"] = 24,
    ["addr"] = "BeiJing",
    ["email"] = "xxxx@example.com",
    ["tel"] = {f='123',d='456'}
}
print(cjson2.encode(lua_object))

lua test.lua

posted @ 2020-08-27 22:01  呓语i  阅读(405)  评论(0编辑  收藏  举报