使用luarocks安装cjson并使用cjson

1.luarocks安装

cpp 复制代码
 wget https://luarocks.org/releases/luarocks-3.3.1.tar.gz --no-check-certificate
 ls -lrth
 tar -xvf luarocks-3.3.1.tar.gz 
 mv luarocks-3.3.1 /usr/local/
 cd /usr/local/luarocks-3.3.1/
 ./configure --prefix=/usr/local/luarocks-3.3.1 
 vim /etc/profile
 PATH=$PATH:/usr/local/luarocks-3.3.1/bin
 source /etc/profile
 luarocks --version

[root@localhost bin]# luarocks --version
/usr/local/luarocks-3.3.1/bin/luarocks 3.3.1
LuaRocks main command-line interface

2.cjson安装

cpp 复制代码
[root@localhost bin]# luarocks install lua-cjson
Installing https://luarocks.org/lua-cjson-2.1.0.10-1.src.rock

lua-cjson 2.1.0.10-1 depends on lua >= 5.1 (5.1-1 provided by VM)
gcc -O2 -fPIC -I/usr/local/include/luajit-2.0 -c lua_cjson.c -o lua_cjson.o
gcc -O2 -fPIC -I/usr/local/include/luajit-2.0 -c strbuf.c -o strbuf.o
gcc -O2 -fPIC -I/usr/local/include/luajit-2.0 -c fpconv.c -o fpconv.o
gcc -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o
No existing manifest. Attempting to rebuild...
lua-cjson 2.1.0.10-1 is now installed in /usr/local/luarocks-3.3.1/bin/./../lua_modules (license: MIT)

/usr/local/luarocks-3.3.1/lua_modules

3.test-encode.lua

cpp 复制代码
[root@localhost lua_modules]# more test-encode.lua 
package.cpath = package.cpath .. ";/usr/local/luarocks-3.3.1/lua_modules/lib/lua/5.1/?.so"

local cjson = require "cjson"
local cjson2 = cjson.new()
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"] = "1569989xxxx@126.com",
    ["tel"] = "1569989xxxx"
}
print(cjson2.encode(lua_object))
[root@localhost lua_modules]# pwd
/usr/local/luarocks-3.3.1/lua_modules
[root@localhost lua_modules]# lua test-encode.lua 
true
[1,2,3,4,5,6]
6.66
"I am test1280"
{"addr":"BeiJing","name":"Jiang","tel":"1569989xxxx","age":24,"email":"1569989xxxx@126.com"}
[root@localhost lua_modules]# 

4.test-decode.lua

cpp 复制代码
[root@localhost lua_modules]# more test-decode.lua
package.cpath = package.cpath .. ";/usr/local/luarocks-3.3.1/lua_modules/lib/lua/5.1/?.so"
local cjson = require "cjson"
local cjson2 = cjson.new()

local json_bool = "false"
local lua_bool = cjson2.decode(json_bool)
print(type(lua_bool))

local json_array = "[9, 8, 7, 6, 5, 4, 3, 2, 1]"
local lua_array = cjson2.decode(json_array)
print(type(lua_array))

for k, v in ipairs(lua_array) do
    print("k = " .. k .. ",   v = "..v)
end

local json_number = "666.666"
local lua_number = cjson2.decode(json_number)
print(type(lua_number))
print(lua_number)

local json_string = "\"I am test1280\""
local lua_string = cjson2.decode(json_string)
print(type(lua_string))
print(lua_string)

local json_object = "{\"name\":\"Jiang\",\"addr\":\"BeiJing\",\"age\":24,\"tel\":\"1569989xxxx\",\"email\":\"1569989xxxx@126.com\"}"
local lua_object = cjson2.decode(json_object)
print(type(lua_object))

for k, v in pairs(lua_object) do
    print("k = " .. k .. ",   v = "..v)
end

[root@localhost lua_modules]# lua test-decode.lua 
boolean
table
k = 1,   v = 9
k = 2,   v = 8
k = 3,   v = 7
k = 4,   v = 6
k = 5,   v = 5
k = 6,   v = 4
k = 7,   v = 3
k = 8,   v = 2
k = 9,   v = 1
number
666.666
string
I am test1280
table
k = addr,   v = BeiJing
k = name,   v = Jiang
k = tel,   v = 1569989xxxx
k = age,   v = 24
k = email,   v = 1569989xxxx@126.com
相关推荐
Tester_孙大壮6 分钟前
了解智能运维
运维
林农38 分钟前
C05S14-MySQL高级语句
linux·mysql·云计算
码农君莫笑1 小时前
《信管通低代码信息管理系统开发平台》Windows环境安装说明
服务器·数据库·windows·低代码·c#·bootstrap·.netcore
Wanliang Li1 小时前
Linux电源管理——CPU Hotplug 流程
linux·嵌入式硬件·嵌入式·armv8·电源管理·cpuhotplug
fnd_LN1 小时前
Linux文件目录 --- mkdir命令,创建目录,多级目录,设置目录权限
linux·运维·服务器
会飞的土拨鼠呀1 小时前
Flannel是什么,如何安装Flannel
运维·云原生·kubernetes
木与子不厌1 小时前
微服务自定义过滤器
运维·数据库·微服务
达帮主2 小时前
7.C语言 宏(Macro) 宏定义,宏函数
linux·c语言·算法
行思理2 小时前
Linux 下SVN新手操作手册
linux·运维·svn
初学者丶一起加油2 小时前
C语言基础:指针(数组指针与指针数组)
linux·c语言·开发语言·数据结构·c++·算法·visual studio