1.luarocks安装
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安装
[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 文章来源:https://www.toymoban.com/news/detail-640831.html
[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文章来源地址https://www.toymoban.com/news/detail-640831.html
[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
到了这里,关于使用luarocks安装cjson并使用cjson的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!