首页 > 技术文章 > lua 的 cjson 安装,使用

chenpython123 2019-09-25 16:23 原文

1. 背景:

  虚拟机安装的luajit  没有 cjson 库,就不能对 table 进行 编码操作,手动安装一个。

2. 安装:

  cjson下载地址:http://www.kyne.com.au/~mark/software/lua-cjson.php

  下载文件 lua-cjson-2.1.0.tar.gz

  放到虚拟机一个目录,解压,先 make 一下

cc -c -O3 -Wall -pedantic -DNDEBUG  -I/usr/local/include -fpic -o lua_cjson.o lua_cjson.c
lua_cjson.c:43:17: error: lua.h: No such file or directory
lua_cjson.c:44:21: error: lauxlib.h: No such file or directory
lua_cjson.c:192: error: expected ‘)’ before ‘*’ token
lua_cjson.c:206: error: expected ‘)’ before ‘*’ token
lua_cjson.c:218: error: expected ‘)’ before ‘*’ token
lua_cjson.c:237: error: expected ‘)’ before ‘*’ token
lua_cjson.c:266: error: expected ‘)’ before ‘*’ token
lua_cjson.c:279: error: expected ‘)’ before ‘*’ token

  这个报错是没有找到 lua 源码,find 一下lua.h这个文件,发现位于好几个路径下。因为历史原因,都可以使用,选择一个。

/usr/local/src/LuaJIT-2.1.0-beta2/src/lua.h
/usr/local/luajit/include/luajit-2.1/lua.h
/usr/local/include/luajit-2.0/lua.h

  修改Makefile文件,修改default配置为

LUA_VERSION =       5.1
TARGET =            cjson.so
PREFIX =            /usr/local
#CFLAGS =            -g -Wall -pedantic -fno-inline
CFLAGS =            -O3 -Wall -pedantic -DNDEBUG
CJSON_CFLAGS =      -fpic
CJSON_LDFLAGS =     -shared
LUA_INCLUDE_DIR =   /usr/local/src/LuaJIT-2.1.0-beta2/src/
LUA_CMODULE_DIR =   $(PREFIX)/lib/lua/$(LUA_VERSION)
LUA_MODULE_DIR =    $(PREFIX)/share/lua/$(LUA_VERSION)
LUA_BIN_DIR =       $(PREFIX)/bin

  主要是修改了LUA_INCLUDE_DIR用于安装cjson;修改了PREFIX变量用来改变编译结果文件输出的路径。

  保存,执行命令

make && make install
cc -c -O3 -Wall -pedantic -DNDEBUG  -I/usr/local/src/LuaJIT-2.1.0-beta2/src/ -fpic -o lua_cjson.o lua_cjson.c
cc -c -O3 -Wall -pedantic -DNDEBUG  -I/usr/local/src/LuaJIT-2.1.0-beta2/src/ -fpic -o strbuf.o strbuf.c
cc -c -O3 -Wall -pedantic -DNDEBUG  -I/usr/local/src/LuaJIT-2.1.0-beta2/src/ -fpic -o fpconv.o fpconv.c
cc  -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o

cp cjson.so /usr/local/lib/lua/5.1/
chmod 755 /usr/local/lib/lua/5.1/cjson.so

 

3. 使用:

 

  编码:

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"] = "1569989xxxx@126.com",
    ["tel"] = {f='123',d='456'}
}
print(cjson2.encode(lua_object))

===================================

 true
 [1,2,3,4,5,6]
 6.66
 "I am test1280"
 {"addr":"BeiJing","tel":{"d":"456","f":"123"},"age":24,"name":"Jiang","email":"1569989xxxx@126.com"}

  解码:

json = require "cjson"

a= '{"deviceid": "dev","taskid": "","tasktype": "3101","configlist":{"sd_new_old_cp_switch":{"vod":"gitvlive"}}}'
atb=json.decode(a)
print(atb.deviceid)
print(atb.configlist)
print(atb.configlist.sd_new_old_cp_switch.vod)

  一般 用的比较多的就是 把 json 字符串 decode 成 lua 的 table 数据类型。

推荐阅读