今天初次用cjson这个工具,特郁闷的就是decode()这个函数。上网看是直接返回一个table,table里面有键值对。然而我调用ngx.say()来打印是会出错的。
错误示范如下:
local json_text = '[{"foo":"bar"}]'
local data = cjson.decode(json_text)
ngx.say("foo: ", data["foo"]) --data.foo is error too!
正确示范如下:
local json_text = '[{"foo":"bar"}]'
local data = cjson.decode(json_text)
ngx.say("foo: ", data[1]["foo"])