发布于 

MoonScript调用LUA-CJSON

作者:糖果

Lapis的util库有对cjson封装,而我们想更直接的调用CJSON的方法,而不想依赖的封装。

我们首先实现一个MoonScript写文件的代码:

写文件:

1
2
3
4
5
6
7
8
9
10

write_data = (var, rule)->
path = "/home/xxx"
file = io.open(path,"aw")
if file==nil then
return

ret = file\write(rule)
file\close()
return(t)

访问接口:

1
2
3
4
5
6
7
8
restyhttp = require "resty.http"
httpc = restyhttp.new()
res, err = httpc\request_uri("http://0.0.0.0/getjson")
jsonbody = ""
if res.status == ngx.HTTP_OK
jsonbody = res.body
else
jsonbody = nil

对JSON数据DECODE:

1
2
3
4
5
write_data(0, jsonbody)
t = json.decode(jsonbody)
tjson = json.decode(t['message'])
ngx.say(util.serialise_value(tjson))
""

本站由 @anonymity 使用 Stellar 主题创建。