local str = json.encode({[3]=1,[5]=2,[6]="3",[7]=4})
ngx.say(str); ---- [null,null,1,null,2,"3",4]
ngx.say("<br/>");
local str = json.encode({[3]=2,[5]=3})
ngx.say(str); ---- [null,null,2,null,3]
ngx.say("<br/>");
table = json.decode(string)
local str = [[ {"a":"v","b":2,"c":{"c1":1,"c2":2},"d":[10,11],"1":100} ]]
local t = json.decode(str)
ngx.say(" --> ", type(t))
local str = [[ {"a":1,"b":null} ]]
local t = json.decode(str)
ngx.say(t.a, "<br/>")
ngx.say(t.b == nil, "<br/>")
ngx.say(t.b == json.null, "<br/>")
----> 1
----> false
----> true
local json = require("cjson")
local str = [[ {"key:"value"} ]]---少了一个双引号
local t = json.decode(str)
ngx.say(" --> ", type(t))
local json = require("cjson")
local function _json_decode(str)
return json.decode(str)
end
function json_decode( str )
local ok, t = pcall(_json_decode, str)
if not ok then
return nil
end
return t
end
local str = [[ {"key:"value"} ]]---少了一个双引号
local t = json_decode(str)
ngx.say(t)
local json = require("cjson.safe")
local str = [[ {"key:"value"} ]]
local t = json.decode(str)
if t then
ngx.say(" --> ", type(t))
else
ngx.say("t is nil")
end
local json = require("cjson")
ngx.say("value --> ", json.encode({}))
local json = require("cjson")
json.encode_empty_table_as_object(false)
ngx.say("value --> ", json.encode({}))