2016-03-16 11:49 GMT+08:00 罗厚付 <luo...@hotmail.com>:
> 再请教一下,这个具体要如何用。
+=== TEST 7: empty_array_mt and empty tables as objects (explicit)
+--- lua
+local cjson = require "cjson"
+cjson.encode_empty_table_as_object(true)
+local empty_arr = setmetatable({}, cjson.empty_array_mt)
+local data = {
+ arr = empty_arr,
+ foo = {
+ obj = {},
+ foobar = {
+ arr = cjson.empty_array,
+ obj = {}
+ }
+ }
+}
+print(cjson.encode(data))
+--- out
+{"foo":{"foobar":{"obj":{},"arr":[]},"obj":{}},"arr":[]}
>
> 在 2016年3月15日星期二 UTC+8下午6:30:38,doujiang写道:
>>
>> Hello,
>>
>> 正巧,春哥今天合了一个 PR[1],正好是你要的
>> 你可以使用这个 repo 的 master,或者等春哥的下一个发布版本
>>
>> 1. https://github.com/openresty/lua-cjson/pull/6
>>
>> 在 2016年3月15日 上午11:46,罗厚付 <luoh...@hotmail.com>写道:
>>>
>>> 我想要处理json里能同时输出{},[]
>>> 当注释cjson.encode_empty_table_as_object(false)时
>>> 输出:{"NULL":null,"empty":"","data":3,"code":{},"specs":{}}
>>> 不注释时
>>> 输出:{"NULL":null,"empty":"","data":3,"code":[],"specs":[]}
>>>
>>>
>>> lua代码
>>> function Test()
>>> local cjson = require("cjson")
>>> cjson.encode_empty_table_as_object(false)
>>> local res1_body =
>>> cjson.decode('{"code":{},"specs":[],"empty":"","NULL":null,"data":1}')
>>> res1_body.data = 3
>>> local res = cjson.encode(res1_body)
>>> ngx.say(res)
>>> end
>>>
>>> 请问有办法解决吗?
>>>
>>> --
>>> --
> --
YuanSheng Wang
---------------------------------------
My Github: https://github.com/membphis
OpenResty lover ^_^