--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
Hello,
lua里面没区分{}和[],春哥合并的pr只解决了encode的问题,并没有解决decode的问题。
encode_empty_table_as_object
使用这个
lua里面没区分{}和[],春哥合并的pr只解决了encode的问题,并没有解决decode的问题。
function json_encode( data, empty_table_as_object )
--Lua的数据类型里面,array和dict是同一个东西。对应到json encode的时候,就会有不同的判断
--对于linux,我们用的是cjson库:A Lua table with only positive integer keys of type number will be encoded as a JSON array. All other tables will be encoded as a JSON object.
--cjson对于空的table,就会被处理为object,也就是{}
--dkjson默认对空table会处理为array,也就是[]
--处理方法:对于cjson,使用encode_empty_table_as_object这个方法。文档里面没有,看源码
--对于dkjson,需要设置meta信息。local a= {};a.s = {};a.b='中文';setmetatable(a.s, { __jsontype = 'object' });ngx.say(comm.json_encode(a))
local json_value = nil
if json.encode_empty_table_as_object then
json.encode_empty_table_as_object(empty_table_as_object or false) -- 空的table默认为array
end
if require("ffi").os ~= "Windows" then
json.encode_sparse_array(true)
end
pcall(function (data) json_value = json.encode(data) end, data)
return json_value
end